| | |
Upper Case Letters
Thread Solved |
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.
[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.
You can use Python's regular expression module re, very powerful for text processing, but there is a somewhat steep learning curve ahead!
python Syntax (Toggle Plain Text)
# exploring Python's regular expression module re import re # find all upper case letters in a text: text = "This text has Upper and Lower Case Letters" all_uppers = re.findall("[A-Z]", text) # make elements unique and sorted all_uppers = sorted(list(set(all_uppers))) print all_uppers # ['C', 'L', 'T', 'U']
May 'the Google' be with you!
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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
[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
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]
[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
![]() |
Similar Threads
- convert lower case letters to uppercase and vice-versa (C++)
- copy string and convert to upper case (Assembly)
- How DO you count Upper Case letters? (Java)
Other Threads in the Python Forum
- Previous Thread: how do I pass values between classes?
- Next Thread: help with treeview property setting
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile shebang slicenotation split ssh string strings table terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wx.wizard wxpython xlwt






