[Solved] Words, sorted by frequency, in a book (.txt file)


if you want to write it in a sorted manner you can do this.

from collections import Counter
wordlist = open('so.py', 'r').read().split()
word_counts = Counter(wordlist)

write_file = open('wordfreq.txt', 'w')
for w, c in sorted(word_counts.iteritems(), key=lambda x: x[1], reverse=True):
    write_file.write('{w}, {c}\n'.format(w=w, c=c))

solved Words, sorted by frequency, in a book (.txt file)