标签:
Counter类提供一个方便和快速统计的工具。
例子:
#python 3.4
import collections
cnt = collections.Counter()
for word in [‘red‘, ‘blue‘, ‘red‘, ‘green‘, ‘blue‘, ‘blue‘]:
cnt[word] += 1
print(cnt)
结果输出如下:
Counter({‘blue‘: 3, ‘red‘: 2, ‘green‘: 1})
从这个例子里可以看到,对单词进行统计,不需要分别记住不同单词进行增加,只调用一个简单的动作,就可以完成了。
class collections.Counter([iterable-or-mapping])
Counter类是继承字典类,并且基于HASH保存。因而它是不排序的容器,把元素当作键,而把计数当作键值。计数的数值是整数,可以是0和负数。
创建一个Counter对象时,可以从迭代对象里创建,也可以从一个映射对象,或者Counter对象创建。
例子:
#python 3.4
import collections
cnt = collections.Counter()
print(cnt)
cnt = collections.Counter(‘this is for test‘)
print(cnt)
cnt = collections.Counter({‘red‘: 4, ‘blue‘: 2})
print(cnt)
cnt = collections.Counter(cats = 4, dogs = 10)
print(cnt)
结果输出如下:
Counter()
Counter({‘t‘: 3, ‘s‘: 3, ‘ ‘: 3, ‘i‘: 2, ‘r‘: 1, ‘f‘: 1, ‘h‘: 1, ‘o‘: 1, ‘e‘: 1})
Counter({‘red‘: 4, ‘blue‘: 2})
Counter({‘dogs‘: 10, ‘cats‘: 4})
在使用Counter对象时要注意两点:1. 当访问一个不存在的元素时,它返回0值,而不是抛出异常。2. 设置一个元素为0值,并不是把这个元素删除,而需要调用del来把元素删除。
例子:
#python 3.4
import collections
cnt = collections.Counter({‘red‘: 4, ‘blue‘: 2})
print(cnt)
print(cnt[‘white‘])
cnt[‘red‘] = 0
print(cnt)
print(‘开始删除:‘)
del cnt[‘red‘]
print(cnt)
结果输出如下:
Counter({‘red‘: 4, ‘blue‘: 2})
0
Counter({‘blue‘: 2, ‘red‘: 0})
开始删除:
Counter({‘blue‘: 2})
Counter类除了支持字典类所有方法外,还增加下面三个方法:
elements()
返回一个迭代器,按每个元素的统计的次数来返回多少次这个元素的值。元素返回的顺序是随机的,并且当元素统计的值为0时不输出此元素。
例子:
#python 3.4
import collections
cnt = collections.Counter(a=4, b=2, c=0, d=-2)
print(cnt)
print(list(cnt.elements()))
结果输出如下:
Counter({‘a‘: 4, ‘b‘: 2, ‘c‘: 0, ‘d‘: -2})
[‘a‘, ‘a‘, ‘a‘, ‘a‘, ‘b‘, ‘b‘]
most_common([n])
返回统计频率最高的前n个元素。如果n没有指定,就返回所有元素。返回的顺序会是从大到小的顺序。
例子:
#python 3.4
import collections
cnt = collections.Counter(a=4, b=2, c=0, d=-2)
print(cnt)
print(cnt.most_common(2))
结果输出如下:
Counter({‘a‘: 4, ‘b‘: 2, ‘c‘: 0, ‘d‘: -2})
[(‘a‘, 4), (‘b‘, 2)]
subtract([iterable-or-mapping])
Counter元素与另外一个迭代对象或映射对象进行相同元素相减的操作。
例子:
#python 3.4
import collections
cnt1 = collections.Counter(a=4, b=2, c=0, d=-2)
cnt2 = collections.Counter(a=1, b=2, c=3, d=4)
print(cnt1)
print(cnt2)
cnt1.subtract(cnt2)
print(cnt1)
结果输出如下:
Counter({‘a‘: 4, ‘b‘: 2, ‘c‘: 0, ‘d‘: -2})
Counter({‘d‘: 4, ‘c‘: 3, ‘b‘: 2, ‘a‘: 1})
Counter({‘a‘: 3, ‘b‘: 0, ‘c‘: -3, ‘d‘: -6})
fromkeys(iterable)
这个方法在 Counter对象没有实现。
update([iterable-or-mapping])
从一个迭代器或者另一个映射对象里添加元素的统计值。
例子:
#python 3.4
import collections
cnt1 = collections.Counter(a=4, b=2, c=0, d=-2)
cnt2 = collections.Counter(a=1, b=2, c=3, d=4)
print(cnt1)
print(cnt2)
cnt1.update(cnt2)
print(cnt1)
结果输出如下:
Counter({‘a‘: 4, ‘b‘: 2, ‘c‘: 0, ‘d‘: -2})
Counter({‘d‘: 4, ‘c‘: 3, ‘b‘: 2, ‘a‘: 1})
Counter({‘a‘: 5, ‘b‘: 4, ‘c‘: 3, ‘d‘: 2})
对Counter对象进行一些公共的操作:
sum(c.values()) # 计算所有计数。
c.clear() # 复位所有计数。
list(c) # 生成列表
set(c) # 生成集合
dict(c) # 生成普通字典
c.items() # 返回(elem, cnt)对的列表
Counter(dict(list_of_pairs)) # 从(elem, cnt)的列表创建Counter
c.most_common()[:-n-1:-1] # n个最常用元素返回
+c # 删除0和负数计数
Counter对象的算术运算:
#python 3.4
import collections
cnt1 = collections.Counter(a=4, b=2, c=0, d=-2)
cnt2 = collections.Counter(a=1, b=2, c=3, d=4)
print(cnt1)
print(cnt2)
print(‘cnt1 + cnt2:‘, cnt1 + cnt2)
print(‘cnt1 - cnt2:‘, cnt1 - cnt2)
print(‘cnt1 & cnt2:‘, cnt1 & cnt2)
print(‘cnt1 | cnt2:‘, cnt1 | cnt2)
print(‘+cnt1:‘, +cnt1)
print(‘-cnt1:‘, -cnt1)
结果输出如下:
Counter({‘a‘: 4, ‘b‘: 2, ‘c‘: 0, ‘d‘: -2})
Counter({‘d‘: 4, ‘c‘: 3, ‘b‘: 2, ‘a‘: 1})
cnt1 + cnt2: Counter({‘a‘: 5, ‘b‘: 4, ‘c‘: 3, ‘d‘: 2})
cnt1 - cnt2: Counter({‘a‘: 3})
cnt1 & cnt2: Counter({‘b‘: 2, ‘a‘: 1})
cnt1 | cnt2: Counter({‘d‘: 4, ‘a‘: 4, ‘c‘: 3, ‘b‘: 2})
+cnt1: Counter({‘a‘: 4, ‘b‘: 2})
-cnt1: Counter({‘d‘: 2})
蔡军生 QQ;9073204 深圳
标签:
原文地址:http://blog.csdn.net/caimouse/article/details/50407432