collections模块中的的Counter类是一个容器类型,可以用来对数据出现的次数进行计数统计。例如:
>>> from collections import Counter
>>> words = "This is for the test of Counter class".split()
>>> cnt = Counter()
>>> for word in words:
cnt[word] += 1
>>> cnt
Counter({'for': 1, 'This': 1, 'of': 1, 'is': 1, 'Counter': 1, 'test': 1, 'the': 1, 'class': 1})
Counter类型的构造函数如下:
classcollections.Counter([iterable-or-mapping])文档中对Counter类型的说明如下:
A Counter is a dict subclassfor counting hashable objects. It is an unordered collection whereelements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. TheCounter class is similar to bags or multisets in other languages.
例如:
c = Counter()
c = Counter('gallant')
c = Counter({'red':4,'blue':2})
c = Counter(cats = 4, dogs = 8)Counter类型拥有字典类型的函数接口,并且对于不存在于Counter对象中的键返回0而不是引发一个TypeError。Counter对象除了支持字典对象的方法另外有三个方法可用:
elements()
Return an iterator over elements repeating each as many times as its count. Elements are returned in arbitrary order. If an element’s count is less than one,elements() will ignore it.
>>> c = Counter('gallant')
>>> c
Counter({'a': 2, 'l': 2, 't': 1, 'g': 1, 'n': 1})
>>> c.elements()
<itertools.chain object at 0x0123EE10>
>>> list(c.elements())
['a', 'a', 't', 'l', 'l', 'g', 'n']
most_common([n])
>>> c.most_common(2)
[('a', 2), ('l', 2)]
subtract([iterable-or-mapping])
>>> c1 = Counter({'a':4,'b':3,'c':1,'d':2})
>>> c2 = Counter({'a':1,'b':1,'c':1,'d':4})
>>> c1.subtract(c2)
Counter({'a': 3, 'b': 2})Counter没有实现dict类型的fromkeys方法,并且update()方法的工作方式也不同于dict类型:
>>> c2.update(c2)
>>> c2
Counter({'d': 8, 'a': 2, 'c': 2, 'b': 2})常见类型与Counter对象协作方式:
sum(c.values()) # total of all counts
c.clear() # reset all counts
list(c) # list unique elements
set(c) # convert to a set
dict(c) # convert to a regular dictionary
c.items() # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1] # n least common elements
c += Counter() # remove zero and negative counts
另外,Counter类型也支持一些数学运算符:
>>> c = Counter(a=3, b=1)
>>> d = Counter(a=1, b=2)
>>> c + d # add two counters together: c[x] + d[x]
Counter({'a': 4, 'b': 3})
>>> c - d # subtract (keeping only positive counts)
Counter({'a': 2})
>>> c & d # intersection: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
>>> c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
REF:Python 2.7.8 documentation
本文介绍Python中collections模块中的Counter类,一个用于对数据出现次数进行计数统计的容器类型。通过实例展示了如何使用Counter类进行数据处理,包括元素迭代、最常见元素查找、减法操作等特性。
750

被折叠的 条评论
为什么被折叠?



