# Frequency count, version 1.1 # Input: dictionary of six letter words # Process: determine the frequency of each letter across the entire dictionary # Output: each letter A-Z and their total number of appearances in all words # This version uses a "list comprehension" from string import lowercase def read_words(fname): # infile = open(fname) # temp = infile.read() # infile.close() # return temp.split('\n')[:-1] infile=open(fname) templist=[] word=infile.readline() while word: templist.append(word[0:len(word)-1]) # strip off the end of line '\n' from the end of word word=infile.readline() return templist def main(): wlist = read_words("wordsShort.txt") # ht = dict(zip(lowercase, [0]*26)) # hash table lowercase = "abcdefghijklmnopqrstuvwxyz" # ht=dict(zip(lowercase,[0]*26)) # hash table ht={} #hash table for i in range(26): ht[lowercase[i]] = 0; for w in wlist: for ch in w: ht[ch] += 1 print "ht: %s" % ht pairs=[] #pairs = [ (ht[ch], ch) for ch in lowercase[0:26] ] # list comprehension for ch in lowercase: pairs.append((ht[ch], ch)) # append the tuple print "pairs: %s" % pairs pairs.sort() # sorts by the frequency (first in pair). If frequencies # are tied, then sorts by letter pairs.reverse() # descending for (x, y) in pairs: print "%s %4d" % (y, x) if __name__=='__main__': main() # Notes # ------ # 1. Loop over words in dictionary (5K), loop over letters in word (6), # increment count for that letter (30K total) # 2. Loop over letters (26), loop over all words (5K), loop over letters # in word (6), count occurrences of current letter (780K total) #