Efficiently calculate word frequency in a string

后端 未结 3 1171
遥遥无期
遥遥无期 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:53

    >>>> test = """abc def-ghi jkl abc
    abc"""
    >>> from collections import Counter
    >>> words = Counter()
    >>> words.update(test.split()) # Update counter with words
    >>> words.most_common()        # Print list with most common to least common
    [('abc', 3), ('jkl', 1), ('def-ghi', 1)]
    

提交回复
热议问题