Advertisement
fmasanori

Word Count

Aug 23rd, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. import string
  2.  
  3. texto = open('alice.txt').read().lower()
  4. for c in string.punctuation:
  5.     texto = texto.replace(c, ' ')
  6.  
  7. palavras = texto.split()
  8.  
  9. wc = {}
  10. for p in palavras:
  11.     if p in wc:
  12.         wc[p] += 1
  13.     else:
  14.         wc[p] = 1
  15.  
  16. def contador(dupla):
  17.     return dupla[1]
  18.  
  19. duplas = sorted(wc.items(), key=contador, reverse=True)
  20. for dupla in duplas[:20]:
  21.     print (dupla[0], dupla[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement