Counting unique words in python

前端 未结 3 914
深忆病人
深忆病人 2020-12-19 10:15

In direct, my code so far is this :

from glob import glob
pattern = \"D:\\\\report\\\\shakeall\\\\*.txt\"
filelist = glob(pattern)
def countwords(fp):
    w         


        
3条回答
  •  情书的邮戳
    2020-12-19 10:37

    The best way to count objects in Python is to use collections.Counter class, which was created for that purposes. It acts like a Python dict but is a bit easier in use when counting. You can just pass a list of objects and it counts them for you automatically.

    >>> from collections import Counter
    >>> c = Counter(['hello', 'hello', 1])
    >>> print c
    Counter({'hello': 2, 1: 1})
    

    Also Counter has some useful methods like most_common, visit documentation to learn more.

    One method of Counter class that can also be very useful is update method. After you've instantiated Counter by passing a list of objects, you can do the same using update method and it will continue counting without dropping old counters for objects:

    >>> from collections import Counter
    >>> c = Counter(['hello', 'hello', 1])
    >>> print c
    Counter({'hello': 2, 1: 1})
    >>> c.update(['hello'])
    >>> print c
    Counter({'hello': 3, 1: 1})
    

提交回复
热议问题