Advertisement
Guest User

histogram

a guest
Feb 18th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. with open('./words.txt', 'r') as words_file:
  2.     words = words_file.read().split(" ")
  3.     unique_words = set(words)
  4.     count = 0
  5.     word_dict = {}
  6.     total = 0
  7.  
  8.     for word in unique_words:
  9.         if len(word) == 0:
  10.             continue
  11.  
  12.         w_count = words.count(word)
  13.         word_dict[word] = w_count
  14.         total += w_count
  15.         count += 1
  16.  
  17.     print('Number of unique words: %s' % count)
  18.    
  19.     for k in word_dict.keys():
  20.         print('%s: %s occurances, %s percent of words' % (k, word_dict[k], round(word_dict[k] / total * 10000) / 100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement