Sorted Word frequency count using python

AlgoMan picture AlgoMan · Nov 3, 2010 · Viewed 78.4k times · Source

I have to count the word frequency in a text using python. I thought of keeping words in a dictionary and having a count for each of these words.

Now if I have to sort the words according to # of occurrences. Can i do it with same dictionary instead of using a new dictionary which has the key as the count and array of words as the values ?

Answer

jathanism picture jathanism · Nov 3, 2010

WARNING: This example requires Python 2.7 or higher.

Python's built-in Counter object is exactly what you're looking for. Counting words is even the first example in the documentation:

>>> # Tally occurrences of words in a list
>>> from collections import Counter
>>> cnt = Counter()
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
...     cnt[word] += 1
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})

As specified in the comments, Counter takes an iterable, so the above example is merely for illustration and is equivalent to:

>>> mywords = ['red', 'blue', 'red', 'green', 'blue', 'blue']
>>> cnt = Counter(mywords)
>>> cnt
Counter({'blue': 3, 'red': 2, 'green': 1})