from collections import Counter计数器

如果要使用 Counter,必须要进行实例化,在实例化的同时可以为构造函数传入参数来指定不同类型的元素来源。
Counter是一个简单的计数器,例如,统计字符出现的个数。
示例:传一个序列,统计出序列中的元素个数

from collections import Counter
c = Counter('woodman')
print(c) # 默认排序倒叙
print(c.most_common(3)) # 获取前三位
print(sum(c.values())) # 汇总统计,相当于len('woodman')

输出:

Counter({'o': 2, 'w': 1, 'd': 1, 'm': 1, 'a': 1, 'n': 1})
[('o', 2), ('w', 1), ('d', 1)]
7

在很多使用到dict和次数的场景下,Python中用Counter来实现会非常简洁,效率也会很高

Counter 中的方法

Counter 类继承自 dict 类,所以 Counter 类可以使用 dict 类的方法。

Counter 特有的方法:
1.elements()

elements()方法返回一个迭代器,可以通过 list 或者其它方法将迭代器中的元素输出,输出的结果为对应出现次数的元素。

c = Counter({'a':1, 'b':2, 'c':3})
c2 = Counter({'a':0, 'b':-1, 'c':3}) # 将出现次数设置为 0 和负值

>>> print(c.elements())
<itertools.chain object at 0x0000022A57509B70> 
>>> print(list(c.elements()))
['a', 'b', 'b', 'c', 'c', 'c'] 

>>> print(c2.elements())
<itertools.chain object at 0x0000022A57509B70> 
>>> print(list(c2.elements()))
['c', 'c', 'c']
2.most_common([n])

返回一个出现次数从大到小的前 n 个元素的列表。

  • 不输入n,默认返回所有;
  • 输入n小于最长长度,则返回前n个数;
  • 输入n等于最长长度,则返回所有;
  • 输入n = -1,则返回空;
c = Counter({'a':1, 'b':2, 'c':3})

>>> print(c.most_common()) # 默认参数
[('c', 3), ('b', 2), ('a', 1)]
>>> print(c.most_common(2)) # n = 2
 [('c', 3), ('b', 2)] 
>>> print(c.most_common(3)) # n = 3
[('c', 3), ('b', 2), ('a', 1)] 
>>> print(c.most_common(-1)) # n = -1
[]
3.subtract([iterable-or-mapping])

将两个 Counter 对象中的元素对应的计数相减。

c = Counter({'a':1, 'b':2, 'c':3})
d = Counter({'a':1, 'b':3, 'c':2, 'd':2})
c.subtract(d)

>>> print(c)
Counter({'c': 1, 'a': 0, 'b': -1, 'd': -2})
字典方法

一般常规的字典方法对 Counter 对象都是有效的,但是在 Counter 中有两个方法和字典中的使用有些区别。

1.fromkeys(iterable)

没有为Counter对象实现该函数

2.update([iterable-or-mapping])

增加count而不是用新的count取代旧的count。

from collections import Counter

c = Counter({'a':1, 'b':2, 'c':3, 'd':0, 'e':-1})

c.update({'a':2, 'd':2, 'e':1})

>>> print(c)
Counter({'a': 3, 'c': 3, 'b': 2, 'd': 2, 'e': 0})

对于 Counter 中的update函数简单来说,就是增加对应元素的计数。

集合运算符
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

示例:

>>> 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})

参考地址:https://docs.python.org/2/library/collections.html#collections.Counter.most_common

  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值