Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from string import letters
- def frequent_words(filename, n):
- wordlist = filter(len, [filter(lambda chr: chr in letters, word.lower()) for word in open(filename).read().split()])
- counts = dict((word, wordlist.count(word)) for word in wordlist)
- return [(word, counts[word]) for word in sorted(counts, key=lambda word: counts[word], reverse=True)][:n]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement