Python 集合中的计数器示例
什么是 Python 柜台?
Python 计数器是一个容器,用于保存容器中每个元素的数量。计数器是字典类中的一个子类。
计数器是字典类中的一个子类。使用 Python 计数器工具,可以统计一个对象(也称为哈希表对象)中的键值对的数量。
为什么使用 Python 柜台?
以下是使用的主要原因 Python 3 计数器:
- Counter 将数据保存在一个无序集合中,就像哈希表对象一样。这里的元素表示键,计数表示值。
- 它允许您计算可迭代列表中的项目数。
- 可以在计数器上轻松执行加法、减法、交集和并集等算术运算。
- 计数器还可以计数来自另一个计数器的元素
简介 Python Counter
Python 计数器接受列表、元组、字典、字符串(都是可迭代对象)的输入,并将输出每个元素的计数。
语法:
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
这是一个简单的例子,展示了计数器模块的工作原理。
from collections import Counter list1 = ['x','y','z','x','x','x','y', 'z'] print(Counter(list1))
输出:
Counter({'x': 4, 'y': 2, 'z': 2})
带字符串的计数器
In Python,一切皆对象,字符串也是对象。 Python 绳子 只需将字符括在双引号中即可创建。 Python 不支持字符类型。这些将被视为长度为 1 的字符串,也被视为子字符串。
在下面的示例中,将一个字符串传递给 Counter。它返回字典格式,其中包含键/值对,其中键是元素,值是计数。它还将空格视为元素并给出字符串中空格的数量。
计费示例:
from collections import Counter my_str = "Welcome to Guru99 Tutorials!" print(Counter(my_str))
输出:
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']。一旦你给列表提供计数器,它就会给你列表中每个元素的计数。
from collections import Counter list1 = ['x','y','z','x','x','x','y','z'] print(Counter(list1))
输出:
Counter({'x': 4, 'y': 2, 'z': 2})
带字典的计数器
字典中的元素是键/值对,并且写在花括号内。
一旦将字典提供给 Counter,它将被转换为哈希表对象,其中元素将成为键,而值将是给定字典中元素的数量。
例如:{'x': 4, 'y': 2, 'z': 2, 'z': 2}。Counter 函数将尝试查找给定字典中每个键的计数。
from collections import Counter dict1 = {'x': 4, 'y': 2, 'z': 2, 'z': 2} print(Counter(dict1))
输出:
Counter({'x': 4, 'y': 2, 'z': 2})
用元组计数器
元组是圆括号内用逗号分隔的对象集合。计数器将给出给定元组中每个元素的数量。
一旦将元组提供给计数器,它将被转换为哈希表对象,其中元素将成为键,而值将是给定元组中元素的数量。
from collections import Counter tuple1 = ('x','y','z','x','x','x','y','z') print(Counter(tuple1))
输出:
Counter({'x': 4, 'y': 2, 'z': 2})
访问、初始化和更新计数器
初始化计数器
可以通过传递字符串值、列表、字典或元组来初始化计数器,如下所示:
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
您还可以初始化一个空的计数器,如下所示:
from collections import Counter _count = Counter()
更新计数器
您可以使用 update() 方法向 Counter 添加值。
_count.update('Welcome to Guru99 Tutorials!')
最终的代码是:
from collections import Counter _count = Counter() _count.update('Welcome to Guru99 Tutorials!') print(_count)
输出是:
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})
访问计数器
要从计数器获取值,您可以执行以下操作:
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]))
输出:
u : 3 G : 1 u : 3 r : 2 u : 3
从计数器中删除元素
要从 Counter 中删除元素,您可以使用 del ,如下例所示:
计费示例:
from collections import Counter dict1 = {'x': 4, 'y': 2, 'z': 2} del dict1["x"] print(Counter(dict1))
输出:
Counter({'y': 2, 'z': 2})
算术运算 Python Counter
可以在计数器上进行加法、减法、交集和并集等算术运算,如下例所示:
计费示例:
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)
输出:
Counter({'y': 7, 'x': 4, 'z': 2}) Counter({'x1': 12, 'x': 4}) Counter({'y': 2}) Counter({'y': 5, 'x': 4, 'z': 4})
可用方法 Python Counter
Counter 有一些重要的方法,下面是它们的列表:
- 元素() :此方法将返回所有计数 >0 的元素。计数为 0 或 -1 的元素将不会被返回。
- 最常见的(值): 此方法将返回计数器列表中最常见的元素。
- 减去(): 此方法用于从另一个 Counter 中扣除元素。
- 更新(): 此方法用于从另一个 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)
输出:
x x x x x y y
例如:most_common(value)
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)]
例如:subtract()
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})
例如:update()
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})
重新分配计数 Python
您可以重新分配计数器的计数,如下所示:
假设你有一本字典:{'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 获取和设置元素数量
要使用 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})
结语
- 计数器是一个容器,它将保存容器中每个元素的计数。
- 计数器是字典类中的一个子类。
- 使用 Python 计数器工具,可以统计对象(也称为哈希表对象)中的键值对的数量。
- Counter 将数据保存在一个无序集合中,就像哈希表对象一样。这里的元素表示键,计数表示值。
- 它允许您计算可迭代列表中的项目数。
- 可以在计数器上轻松执行加法、减法、交集和并集等算术运算。
- 计数器还可以对来自另一个计数器的元素进行计数。
- Counter 上可用的重要的方法是 elements()、most_common(value)、subtract() 和 update()。
- 计数器可用于字符串、列表、字典和元组。