I need to make a program that prints out the frequency of letters in a text file and compares that frequency with that of another in python.
So far I am able to prin
I think this is a really straight forward way to do it:
while True:
speech = raw_input("Enter file name:")
wholeFile = open(speech, 'r+').read()
lowlet = wholeFile.lower()
alphas = 'abcdefghijklmnopqrstuvwxyz'
# lets set default values first
occurrences = {letter : 0 for letter in alphas }
# occurrences = dict(zip(alphas, [0]*len(alphas))) # for python<=2.6
# total number of valid letters
total = 0
# iter everything in the text
for letter in lowlet:
# if it is a valid letter then it is in occurrences
if letter in occurrences:
# update counts
total += 1
occurrences[letter] += 1
# now print the results:
for letter, count in occurrences.iteritems():
print letter, (1.0*count/total)
As you notices you need the total number of valid letters in the text before you can calculate the frequency. Either you filter the text before processing it, or you combine the filtering with the processing, which is what I do here.