A dictionary with the letter as the key is the straight forward way to do this. Otherwise use a function with a for loop to pass the letter and the string to the function.
import string
letter_dict = {}
for letter in string.ascii_uppercase:
letter_dict[letter] = 0
f = [ "abc def\n",
"ghi abc def\n",
"mno stuvw abc\n" ]
for line in f:
for word in line.strip().split():
letter = word[0].upper()
letter_dict[letter] += 1
for letter in string.ascii_uppercase:
print "letter %s = %d" % (letter, letter_dict[letter])
Last edited by woooee; Oct 2nd, 2009 at 12:46 am.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
Offline 2,307 posts
since Dec 2006