Python : 计数器Counter

Collection

from collections import Counter

Init signature: Counter(*args, **kwds)

Docstring:

Dict subclass for counting hashable items. Sometimes called a bag or multiset. Elements are stored as dictionary keys and their counts are stored as dictionary values.

返回值:Counter字典
c = Counter('abcdeabcdabcaba')  # count elements from a string
>>> print(c)    
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
List the n most common elements and their counts from the most common to the least. If n is None, then list all element counts.
>>> c.most_common()        #  没有传递参数时,输出所有的要素
[('a', 5), ('b', 4), ('c', 3), ('d', 2), ('e', 1)]
>>> c.most_common(3)      # three most common elements 最多的3个要素
[('a', 5), ('b', 4), ('c', 3)]
Return a new list containing all items from the iterable in ascending order.
按升序返回包含items的list
>>> sorted(c)              # list all unique elements
['a', 'b', 'c', 'd', 'e']
常规操作
>>> ''.join(sorted(c.elements()))   # list elements with repetitions
'aaaaabbbbcccdde'
>>> sum(c.values())                 # total of all counts
15

>>> c['a']                          # count of letter 'a'
5
>>> for elem in 'shazam':           # update counts from an iterable
...     c[elem] += 1                # by adding 1 to each element's count
>>> c['a']                          # now there are seven 'a'
7
>>> del c['b']                      # remove all 'b'
>>> c['b']                          # now there are zero 'b'
0

>>> d = Counter('simsalabim')       # make another counter
>>> c.update(d)                     # add in the second counter
>>> c['a']                          # now there are nine 'a'
9

>>> c.clear()                       # empty the counter
>>> c
Counter()
If a count is set to zero or reduced to zero, it will remain in the counter until the entry is deleted or the counter is cleared:
如果减少计数器的数值为0(或设定为0),但是元素不会消失,除非完全被释放.
>>> c = Counter('aaabbc')
>>> c['b'] -= 2                     # reduce the count of 'b' by two
>>> c.most_common()                 # 'b' is still in, but its count is zero
[('a', 3), ('c', 1), ('b', 0)]

>>> c = Counter()                           # a new, empty counter
>>> c = Counter('gallahad')                 # a new counter from an iterable
print(c)
Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1})
>>> c = Counter({'a': 4, 'b': 2})           # a new counter from a mapping
>>> c = Counter(a=4, b=2)                   # a new counter from keyword args
Counter({'a': 4, 'b': 2})
>>> c['g'] = 3
>>> c
Counter({'a': 4, 'b': 2, 'g': 3})
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 9,504评论 0 13
  • 函数调用 Built-in Functions abs(x) Return the absolute value ...
    叫我七夜阅读 1,194评论 0 0
  • 人类学习的最根本工具是大脑,离开这个工具,其他学习工具都毫无用途。因此,了解大脑的特点、熟练掌握大脑的应用,将给学...
    山水本原阅读 387评论 0 2
  • 慢慢进入了多雨的雨季,总有很多事情需要处理,总有很多东西需要逼迫自己成长
    东方茂阅读 218评论 0 0
  • 这儿是一座坟墓, 惨淡的月色下, 坟边的荒草格外锐利, 它们吸食了墓下的腐尸。 时刻预谋着, 刺透人的心脏。 一阵...
    余子潇阅读 412评论 4 8