Upper Case Letters

Thread Solved

Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Upper Case Letters

 
0
  #1
Nov 3rd, 2006
I have a text I want to search for all it's upper case letters, then present these letters unique and sorted. I welcome any suggestions.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Upper Case Letters

 
0
  #2
Nov 3rd, 2006
Here's an exampmle:

[php]
alphabet = "abcdefghijklmnopqrstuvwxyz"
text1 = "LQZYdMDHPEdWOAVUCBDdsfTEFgdfGIRKwerJMSONPX"
text2 = ""
upper_letters = []

#P
for letter in text1:
# Check to see if the letter is already in upper_letters
if upper_letters.count(letter) == 1:
continue
# If letter is uppercase, add it to upper_letters
if letter in alphabet.upper():
upper_letters.append(letter)
# Put list in alphabetical order
upper_letters.sort()
# Put list into string with nothing between the letters
text2 = "".join(upper_letters)
# Print the original string and the resulting string.
print "This is the text1 string: '%s'" % (text1)
print "This string contains only the uppercase letters in text1: '%s'" % (text2)
[/php]

This takes a string of random letters, puts each upper case in a list (unless it's already in there), sorts the list, puts it into a string, and then prints it out.

I hope that helped.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,022
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Upper Case Letters

 
0
  #3
Nov 4th, 2006
You can use Python's regular expression module re, very powerful for text processing, but there is a somewhat steep learning curve ahead!
  1. # exploring Python's regular expression module re
  2.  
  3. import re
  4.  
  5. # find all upper case letters in a text:
  6. text = "This text has Upper and Lower Case Letters"
  7. all_uppers = re.findall("[A-Z]", text)
  8.  
  9. # make elements unique and sorted
  10. all_uppers = sorted(list(set(all_uppers)))
  11. print all_uppers # ['C', 'L', 'T', 'U']
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Upper Case Letters

 
0
  #4
Nov 4th, 2006
Or if regular expressions are too painful, some kind of compromise:

[php]
def count_caps(s):
d = {}
caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in caps:
d[i] = s.count(i)
return d

def print_caps(s):
d = count_caps(s)

# not 'for i in d' becuase dictionaries don't have guaranteed order
for i in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
if d[i]:
print i + ":" + str(d[i]),

print_caps("ASDHbajksfhHHLASD")
A:2 D:2 H:3 L:1 S:2
[/php]

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Upper Case Letters

 
0
  #5
Nov 4th, 2006
Thanks Jeff, that is even fancier than I needed. Will come in handy though!

The re module seems to be nice, really don't have much trouble seeing the syntax, but I have a little problem understanding the one liner about making a list unique and sort.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 173
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Upper Case Letters

 
0
  #6
Nov 4th, 2006
My humble contribution using a list comprehension:
[php]# create a unique sorted list of all upper case letters in text
text = "This text has Upper and Lower Case Letters"
unique_list = []
[unique_list.append(c) for c in text if c.isupper() and c not in unique_list]
print sorted(unique_list) # ['C', 'L', 'T', 'U']
[/php]Here is the breakdown of vegaseat's one liner as I see it:
[php]text = "This text has Upper and Lower Case Letters"

uppers_raw = re.findall("[A-Z]", text) # ['T', 'U', 'L', 'C', 'L']
uppers_set = set(uppers_raw) # set(['C', 'U', 'T', 'L'])
uppers_list = list(uppers_set) # ['C', 'U', 'T', 'L']
uppers_sorted = sorted(uppers_list) # ['C', 'L', 'T', 'U']
[/php]
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Upper Case Letters

 
0
  #7
Nov 5th, 2006
So set() removes duplicates?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,022
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Upper Case Letters

 
0
  #8
Nov 5th, 2006
Originally Posted by LaMouche View Post
So set() removes duplicates?
Yes, going fom a list to a set then back to a list removes duplicates from the list, but it will change the order of the elements. So, only use this little trick when order does not matter.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Upper Case Letters

 
0
  #9
Nov 6th, 2006
Looks like this code comes in handy too:
# make elements in the list all_uppers unique and sorted
all_uppers = sorted(list(set(all_uppers)))
Thanks to all who helped!
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC