Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Read a text file and print out a list of the words in order of frequency of occurrence.
- fin = open('py4e-words.txt')
- allfile = fin.read() # read all lines at once
- wordlist = allfile.split() # split the whole of the text into words
- wordset = set(wordlist) # create a set (unique set of words from the text)
- wdict = {}
- # Create a dictionary of each word and its frequency
- for word in wordset:
- wdict[word] = wordlist.count(word)
- # Create a list from the dictionary and sort it by frequency
- blist = []
- for word, ctr in wdict.items():
- blist.append((ctr, word)) # Each list entry is a tuple containing the count and the word
- # Sort the list in reverse order (most frequent first)
- blist.sort(reverse=True)
- # Print the sorted list
- for tup in blist:
- print(tup[1].ljust(14), tup[0])
- # Top entries in the results:-
- # to 16
- # the 6
- # we 5
- # our 5
- # of 5
- # do 5
- # computers 5
- # and 5
- # you 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement