[Python]collections模块中的Counter类
来源:互联网 发布:彩虹秒赞 v7源码 编辑:程序博客网 时间:2024/05/01 19:48
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>>> cntCounter({'for': 1, 'This': 1, 'of': 1, 'is': 1, 'Counter': 1, 'test': 1, 'the': 1, 'class': 1})Counter类型的构造函数如下:
A Counter is a dict subclassforcounting 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')>>> cCounter({'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)>>> c2Counter({'d': 8, 'a': 2, 'c': 2, 'b': 2})常见类型与Counter对象协作方式:
sum(c.values()) # total of all countsc.clear() # reset all countslist(c) # list unique elementsset(c) # convert to a setdict(c) # convert to a regular dictionaryc.items() # convert to a list of (elem, cnt) pairsCounter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairsc.most_common()[:-n-1:-1] # n least common elementsc += 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
0 0
- [Python]collections模块中的Counter类
- Python collections模块Counter类深入分析
- Python collections模块的Counter类
- collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- python之collections模块Counter类使用学习
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python标准库——collections模块的Counter类
- Python中的collections模块
- Python Collections Counter
- python collections Counter
- Python:使用Counter进行计数统计及collections模块
- Python collections中的Counter作用以及源码分析
- collections Counter类
- python Counter模块
- 配音涡轮WAV音频文件与MP3的便携性
- 安卓Theme详解
- 欧美嘉分享旅行中不容错过的“怪异”水果
- (windows,linux)DES加密解密
- 爱狗的法师打发斯蒂芬
- [Python]collections模块中的Counter类
- poj 2524 Ubiquitous Religions
- 阿斯顿发送到发撒的发松岛枫
- Erlang的并行编译与加密
- 六种PHP图片上传重命名方案研究与总结
- poj 2418 Hardwood Species(使用STL map)
- Android 中发一个intent启动多个应用,去掉always菜单技巧
- 阿斯顿发送到发十八式的发生大发
- 2014新生暑假个人排位赛04 C. 崔逗逗的难题