from collections import deque
from collections import Counter
from collections import Counter
# 实例化元素为空的 Counter 对象
a = Counter()
# 传入字符串
b = Counter('chenkc')
# 传入字典
c = Counter({'a':1, 'b':2, 'c':3})
# 传入关键词参数
d = Counter(a = 1, b = 2, c = 3)
elements()
输出Counter中对应出现次数的元素
返回一个迭代器,可以通过 list 或者其它方法将迭代器中的元素输出
c = Counter({'a':1, 'b':2, 'c':3})
>>> print(c.elements())
<itertools.chain object at 0x0000022A57509B70>
>>> print(list(c.elements()))
['a', 'b', 'b', 'c', 'c', 'c']
most_common([n])
是 Counter 最常用的方法,返回一个出现次数从大到小的前 n 个元素的列表。
c = Counter({'a':1, 'b':2, 'c':3})
>>> print(c.most_common(2)) # n = 2
[('c', 3), ('b', 2)]