Efficiently calculate word frequency in a string

后端 未结 3 1193
遥遥无期
遥遥无期 2020-12-10 12:19

I am parsing a long string of text and calculating the number of times each word occurs in Python. I have a function that works but I am looking for advice on whether there

3条回答
  •  无人及你
    2020-12-10 12:45

    Use collections.Counter:

    >>> from collections import Counter
    >>> test = 'abc def abc def zzz zzz'
    >>> Counter(test.split()).most_common()
    [('abc', 2), ('zzz', 2), ('def', 2)]
    

提交回复
热议问题