Advertisement
Guest User

new

a guest
Apr 9th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.41 KB | None | 0 0
  1. def getText():
  2.     txt=open("WPS:\hamlet.txt","r").read()
  3.     txt=txt.lower()
  4.     for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_{|}·~‘’':
  5.         txt=txt.repalce(ch,"  ")
  6. hamletTxt=getText()
  7. words=hamletTxt.split()
  8. counts={}
  9. for word in words:
  10.     counts[word]= counts. get(word, 0)+1
  11. items= list(counts. items())
  12. items.sort(key=lambda x:x[1],reverse=True)
  13. for i in range(10):
  14.     word,count=items[i]
  15. print ("{0: <10}{1:>5}".format(word, count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement