Advertisement
acclivity

pyFindWordFrequency

Nov 19th, 2021
1,161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. # Read a text file and print out a list of the words in order of frequency of occurrence.
  2.  
  3. fin = open('py4e-words.txt')
  4. allfile = fin.read()            # read all lines at once
  5. wordlist = allfile.split()      # split the whole of the text into words
  6. wordset = set(wordlist)         # create a set (unique set of words from the text)
  7.  
  8. wdict = {}
  9. # Create a dictionary of each word and its frequency
  10. for word in wordset:
  11.     wdict[word] = wordlist.count(word)
  12.  
  13. # Create a list from the dictionary and sort it by frequency
  14. blist = []
  15. for word, ctr in wdict.items():
  16.     blist.append((ctr, word))      # Each list entry is a tuple containing the count and the word
  17.  
  18. # Sort the list in reverse order (most frequent first)
  19. blist.sort(reverse=True)
  20.  
  21. # Print the sorted list
  22. for tup in blist:
  23.     print(tup[1].ljust(14), tup[0])
  24.  
  25. # Top entries in the results:-
  26. # to             16
  27. # the            6
  28. # we             5
  29. # our            5
  30. # of             5
  31. # do             5
  32. # computers      5
  33. # and            5
  34. # you            4
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement