Python中的计数器Counter

计数器counter是包含在collections模块中的容器。

什么是容器Container?

容器是容纳对象的对象。它们提供了一种访问所包含对象并对其进行迭代的方法。内置容器的例子有元组、列表和字典。其他内容包含在“collections”模块中。
Counter是dict的子类。因此,它是一个无序的集合,其中元素和它们各自的计数被存储为字典。这相当于一个包或多个其他语言集。

语法

class collections.Counter([iterable-or-mapping])

初始化
计数器的构造函数可以通过以下任一方式调用:

  • 包含一系列项目
  • 使用包含键和计数的字典
  • 带有将字符串名称映射到计数的关键字参数

初始化计数器

# A Python program to show different ways to create
# Counter
from collections import Counter
 
# With sequence of items
print(Counter(['B','B','A','B','C','A','B','B','A','C']))
 
# with dictionary
print(Counter({'A':3, 'B':5, 'C':2}))
 
# with keyword arguments
print(Counter(A=3, B=5, C=2))

输出

Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})
Counter({'B': 5, 'A': 3, 'C': 2})

计数器更新

我们也可以通过以下方式创建一个空计数器:

coun = collections.Counter()

并且可以通过update() 方法进行更新。相同的语法:

coun.update(Data)
# A Python program to demonstrate update()
from collections import Counter
coun = Counter()
 
coun.update([1, 2, 3, 1, 2, 1, 1, 2])
print(coun)
 
coun.update([1, 2, 4])
print(coun)

输出

Counter({1: 4, 2: 3, 3: 1})
Counter({1: 5, 2: 4, 3: 1, 4: 1}

数据可以用初始化中提到的三种方式中的任何一种提供,计数器的数据将增加而不是替换。计数也可以为零或负数。

# Python program to demonstrate that counts in
# Counter can be 0 and negative
from collections import Counter
 
c1 = Counter(A=4,  B=3, C=10)
c2 = Counter(A=10, B=3, C=4)
 
c1.subtract(c2)
print(c1)

输出

 Counter({'c': 6, 'B': 0, 'A': -6})

列表中的唯一计数

我们可以使用Counter来计算列表或其他集合中的不同元素。

# An example program where different list items are
# counted using counter
from collections import Counter
 
# Create a list
z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
 
# Count distinct elements and print Counter aobject
print(Counter(z))

输出

Counter({'blue': 3, 'red': 2, 'yellow': 1})

打印计数器值

我们还可以使用keys() 、values() 和items() 方法访问计数器的所有键和值。这些方法分别返回计数器中的键、值和键值对的视图。

from collections import Counter
my_counter = Counter('abracadabra')
print(my_counter.keys())
print(my_counter.values())
print(my_counter.items())

输出

dict_keys(['a', 'b', 'r', 'c', 'd'])
dict_values([5, 2, 2, 1, 1])
dict_items([('a', 5), ('b', 2), ('r', 2), ('c', 1), ('d', 1)])

访问计数器

一旦初始化,计数器就像字典一样被访问。此外,它不会引发KeyValue错误(如果key不存在),而是值的计数显示为0。

# Python program to demonstrate accessing of
# Counter elements
from collections import Counter

# Create a list
z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
col_count = Counter(z)
print(col_count)

col = ['blue','red','yellow','green']

# Here green is not in col_count
# so count of green will be zero
for color in col:
	print (color, col_count[color])

输出

Counter({'blue': 3, 'red': 2, 'yellow': 1})
blue 3
red 2
yellow 1
green 0

elements()

elements() 方法返回一个迭代器,该迭代器生成Counter已知的所有项。
注意:不包括count <= 0的元素。

# Python example to demonstrate elements() on
# Counter (gives back list)
from collections import Counter

coun = Counter(a=1, b=2, c=3)
print(coun)

print(list(coun.elements()))

输出

Counter({'c': 3, 'b': 2, 'a': 1})
['a', 'b', 'b', 'c', 'c', 'c']

most_common()

most_common() 用于产生N个最频繁遇到的输入值及其相应计数的序列。

# Python example to demonstrate most_elements() on
# Counter
from collections import Counter

coun = Counter(a=1, b=2, c=3, d=120, e=1, f=219)

# This prints 3 most frequent characters
for letter, count in coun.most_common(3):
	print('%s: %d' % (letter, count))

输出

f: 219
d: 120
c: 3

你可能感兴趣的:(python,python)