Counter对容器中存在的每个元素进行计数。 Counter是字典类中可用的子类。
Counter接受输入列表,元组,字典,字符串,它们都是可迭代的对象,它将为您提供输出,其中包含每个元素的计数。
语法
Counter(list)
考虑您有以下列表:
list1 = ['x','y','z','x','x','x','y', 'z']
该列表包含元素x,y和z。当您在此列表上使用Counter时,它将计算存在x,y和z的次数。 如果在list1上使用counter,则输出应类似于:
Counter({'x': 4, 'y': 2, 'z': 2})
因此,我们知道x计数4,y计数2,z计数2
要使用Counter,我们需要首先将其导入:
from collections import Counter
这是一个简单的示例,显示了Counter模块的工作。
from collections import Counter
list1 = ['x','y','z','x','x','x','y', 'z']
print(Counter(list1))
Output:
Counter({'x': 4, 'y': 2, 'z': 2})
在下面的示例中,一个字符串被传递给Counter。 它返回字典格式,带有键/值对,其中键是元素,值是计数。 它还将空格视为元素,并给出字符串中空格的数量。
示例:
from collections import Counter
my_str = "Welcome to Guru99 Tutorials!"
print(Counter(my_str))
Output:
Counter({'o': 3, ' ': 3, 'u': 3, 'e': 2, 'l': 2, 't': 2, 'r': 2, '9': 2, 'W': 1,
'c': 1, 'm': 1, 'G': 1, 'T': 1, 'i': 1, 'a': 1, 's': 1, '!': 1})
列表是一个可迭代的对象,其元素放在方括号中。
列表中的元素在提供给Counter时将被转换为哈希表对象,其中这些元素将成为键,而值将是给定列表中元素的计数。
例如[‘x’,’y’,’z’,’x’,’x’,’x’,’y’,’z’]。 一旦为列表提供了Counter,它将为您提供列表中每个元素的计数。
from collections import Counter
list1 = ['x','y','z','x','x','x','y','z']
print(Counter(list1))
Output:
Counter({'x': 4, 'y': 2, 'z': 2})
将字典提供给计数器后,它将转换为哈希表对象,其中的元素将成为键,而值将是给定字典中元素的计数。
例如:{‘x’:4,’y’:2,’z’:2,’z’:2}。 计数器功能将尝试查找给定词典中每个键的计数。
from collections import Counter
dict1 = {'x': 4, 'y': 2, 'z': 2, 'z': 2}
print(Counter(dict1))
Output:
Counter({'x': 4, 'y': 2, 'z': 2})
元组是用圆括号括起来的用逗号分隔的对象的集合。 Counter将为您提供给定元组中每个元素的计数。
一旦将元组提供给Counter,它将被转换为哈希表对象,其中的元素将成为键,而值将是给定元组中元素的计数。
from collections import Counter
tuple1 = ('x','y','z','x','x','x','y','z')
print(Counter(tuple1))
Output:
Counter({'x': 4, 'y': 2, 'z': 2})
初始化计Counter
可以通过传递字符串值,列表,字典或元组来初始化Counter,如下所示:
from collections import Counter
print(Counter("Welcome to Guru99 Tutorials!")) #using string
print(Counter(['x','y','z','x','x','x','y', 'z'])) #using list
print(Counter({'x': 4, 'y': 2, 'z': 2})) #using dictionary
print(Counter(('x','y','z','x','x','x','y', 'z'))) #using tuple
您还可以初始化一个空的Counter,如下所示:
from collections import Counter
_count = Counter()
更新Counter
您可以使用update()方法将值添加到Counter。
_count.update('Welcome to Guru99 Tutorials!')
最终的代码是:
from collections import Counter
_count = Counter()
_count.update('Welcome to Guru99 Tutorials!')
print(_count)
The output is:
Counter({'o': 3, ' ': 3, 'u': 3, 'e': 2, 'l': 2, 't': 2, 'r': 2, '9': 2, 'W': 1,
'c': 1, 'm': 1, 'G': 1, 'T': 1, 'i': 1, 'a': 1, 's': 1, '!': 1})
访问Counter
要从Counter获取值,可以执行以下操作:
from collections import Counter
_count = Counter()
_count.update('Welcome to Guru99 Tutorials!')
print('%s : %d' % ('u', _count['u']))
print('\n')
for char in 'Guru':
print('%s : %d' % (char, _count[char]))
Output:
u : 3
G : 1
u : 3
r : 2
u : 3
要从Counter中删除元素,可以使用del,如下例所示:
Example:
from collections import Counter
dict1 = {'x': 4, 'y': 2, 'z': 2}
del dict1["x"]
print(Counter(dict1))
输出:
Counter({'y': 2, 'z': 2})
可以在Counter上完成算术运算(例如加法,减法,交集和并集),如下例所示:
Example:
from collections import Counter
counter1 = Counter({'x': 4, 'y': 2, 'z': -2})
counter2 = Counter({'x1': -12, 'y': 5, 'z':4 })
#Addition
counter3 = counter1 + counter2 # only the values that are positive will be returned.
print(counter3)
#Subtraction
counter4 = counter1 - counter2 # all -ve numbers are excluded.For example z will be z = -2-4=-6, since it is -ve value it is not shown in the output
print(counter4)
#Intersection
counter5 = counter1 & counter2 # it will give all common positive minimum values from counter1 and counter2
print(counter5)
#Union
counter6 = counter1 | counter2 # it will give positive max values from counter1 and counter2
print(counter6)
Output:
Counter({'y': 7, 'x': 4, 'z': 2})
Counter({'x1': 12, 'x': 4})
Counter({'y': 2})
Counter({'y': 5, 'x': 4, 'z': 4})
Counter有一些重要的方法:
示例: elements()
from collections import Counter
counter1 = Counter({'x': 5, 'y': 2, 'z': -2, 'x1':0})
_elements = counter1.elements() # will give you all elements with positive value and count>0
for a in _elements:
print(a)
Output:
x
x
x
x
x
y
y
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
common_element = counter1.most_common(2) # The dictionary will be sorted as per the most common element first followed by next.
print(common_element)
common_element1 = counter1.most_common() # if the value is not given to most_common , it will sort the dictionary and give the most common elements from the start.The last element will be the least common element.
print(common_element1)
输出:
[('y', 12), ('x', 5)]
[('y', 12), ('x', 5), ('x1', 0), ('z', -2)]
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
counter2 = Counter({'x': 2, 'y':5})
counter1.subtract(counter2)
print(counter1)
输出:
Counter({'y': 7, 'x': 3, 'x1': 0, 'z': -2})
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
counter2 = Counter({'x': 2, 'y':5})
counter1.update(counter2)
print(counter1)
输出:
Counter({'y': 17, 'x': 7, 'x1': 0, 'z': -2})
您可以如下所示重新分配Counter的计数:
假设您有一个字典,例如:{‘x’:5,’y’:12,’z’:-2,’x1’:0}
您可以如下所示更改元素的计数:
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
counter1['y'] = 20
print(counter1)
输出:执行后,您会看到y计数从12更改为20
Counter({'y': 20, 'x': 5, 'x1': 0, 'z': -2})
要使用Counter获得元素的计数,您可以执行以下操作:
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
print(counter1['y']) # this will give you the count of element 'y'
输出:
12
要设置元素的计数,可以执行以下操作:
from collections import Counter
counter1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1':0})
print(counter1['y'])
counter1['y'] = 20
counter1['y1'] = 10
print(counter1)
输出:
12
Counter({'y': 20, 'y1': 10, 'x': 5, 'x1': 0, 'z': -2})