This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

g

By: a guest on Apr 21st, 2009  |  syntax: Python  |  size: 0.36 KB  |  views: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from string import letters
  2.  
  3. def frequent_words(filename, n):
  4.     wordlist = filter(len, [filter(lambda chr: chr in letters, word.lower()) for word in open(filename).read().split()])
  5.     counts = dict((word, wordlist.count(word)) for word in wordlist)
  6.     return [(word, counts[word]) for word in sorted(counts, key=lambda word: counts[word], reverse=True)][:n]
clone this paste RAW Paste Data