pythonsortingword-frequency

Sorted Word frequency count using python


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 ?


Solution

  • You can use the same dictionary:

    >>> d = { "foo": 4, "bar": 2, "quux": 3 }
    >>> sorted(d.items(), key=lambda item: item[1])
    

    The second line prints:

    [('bar', 2), ('quux', 3), ('foo', 4)]
    

    If you only want a sorted word list, do:

    >>> [pair[0] for pair in sorted(d.items(), key=lambda item: item[1])]
    

    That line prints:

    ['bar', 'quux', 'foo']