Sorted Word frequency count using python

前端 未结 10 908
再見小時候
再見小時候 2020-11-28 06:00

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 so

10条回答
  •  孤城傲影
    2020-11-28 06:33

    >>> d = {'a': 3, 'b': 1, 'c': 2, 'd': 5, 'e': 0}
    >>> l = d.items()
    >>> l.sort(key = lambda item: item[1])
    >>> l
    [('e', 0), ('b', 1), ('c', 2), ('a', 3), ('d', 5)]
    

提交回复
热议问题