#This program counts the number of times that #letters appear in a text document and sorts them #by frequncy. #Used to keep a matching between letters and #the number of times they have been seen. aDict = { } filename = "msnd.txt" f = open(filename, "r") #count of the total number of letters count = 0 letter = f.read(1) while len( letter) > 0 : count = count + 1 letter = letter.lower() #Find out how many times have I seen this letter #and add one to the count, and then #update the dictionary aDict[letter] = aDict.get(letter, 0) + 1 letter = f.read(1) f.close() print("all done!") aList = [] #Get the letters and the counts out of the #dictionary, for key in aDict: value = aDict[key] aTuple = (value, key) aList.append(aTuple) #and sort them by frequency. aList.sort(reverse=True) #Make a nice printed output for item in aList: letter = item[1] numTimes = item[0] #print(letter) print("{:.4f} - {}".format( numTimes/count, letter) )