Want more features on Pastebin? Sign Up, it's FREE!


By: a guest on Apr 21st, 2009  |  syntax: Python  |  size: 0.36 KB  |  views: 119  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print  |  QR code  |  clone
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from string import letters
  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