首页文章手机怎么安装py thon_Python计数器– Py​​thon计数器手机公里计数器「手机怎么安装py thon_Python计数器– Py​​thon计数器」

手机怎么安装py thon_Python计数器– Py​​thon计数器手机公里计数器「手机怎么安装py thon_Python计数器– Py​​thon计数器」

时间2025-02-06 15:29:27发布yu分类文章浏览143
导读:手机怎么安装py thon Python Counter class is part of Collections module. Counter is a subclass of Dictionary and used to keep track of elements and their count....

手机怎么安装py thon

Python Counter class is part of Collections module. Counter is a subclass of Dictionary and used to keep track of elements and their count.

Python Counter类是Collections模块的一部分。 Counter是Dictionary的子类,用于跟踪元素及其数量。

Counter is an unordered collection where elements are stored as keys and their count as dict value.

计数器是一个无序,其中元素存储为键,其计数为dict值。

Counter elements count can be positive, zero or negative integers. However there is no restriction on it’s keys and values. Although values are intended to be numbers but we can store other objects too.

计数器元素count可以为正,零或负整数。 但是,对其键和值没有限制。 尽管值旨在为数字,但我们也可以存储其他对象。

We can create an empty Counter or start with some initial values too.

我们可以创建一个空的Counter或也可以从一些初始值开始。

We can also use any Iterable as argument for creating Counter object. So string literal and List can be used too for creating Counter object.

我们还可以使用任何Iterable作为创建Counter对象的参数。 因此字符串文字和列表也可以用于创建Counter对象。

As I mentioned above, we can use non-numeric data for count values too, but that will defect the purpose of Counter class.

正如我上面提到的,我们也可以将非数字数据用于计数值,但是这会抵消Counter类的用途。

Let’s look into Counter class methods and some other operations we can perform on it.

让我们看一下Counter类的方法以及我们可以对其执行的其他一些操作。

获取元素数 (Getting Count of Elements)

If we try to get the count of non-existing key, it will return 0 and not throw .

如果我们尝试获取不存在的键的计数,它将返回0而不抛出 。

设置元素数 (Setting Count of Elements)

We can also set the count of existing element in the counter. If the element doesn’t exist, then it gets added to the counter.

我们还可以设置计数器中现有元素的计数。 如果该元素不存在,则将其添加到计数器。

从Counter删除元素 (Deleting an element from Counter)

We can use to delete an element from the counter object.

我们可以使用从计数器对象中删除一个元素。

elements() (elements())

This method returns the list of elements in the counter. Only elements with positive counts are returned.

此方法返回计数器中的元素列表。 仅返回具有正计数的元素。

Above code will print “Dog” two times because it’s count is 2. Other elements will be ignored because they don’t have positive count. Counter is an unordered collection, so elements are returned in no particular order.

上面的代码将两次打印“ Dog”,因为它的计数是2。其他元素将因为它们没有正计数而被忽略。 Counter是无序,因此元素没有特定顺序返回。

most_common(n) (most_common(n))

This method returns the most common elements from the counter. If we don’t provide value of ‘n’ then sorted dictionary is returned from most common the least common elements. We can use slicing to get the least common elements on this sorted dictionary.

此方法从计数器返回最常见的元素。 如果我们不提供'n'值,则从最常见的最不常见元素返回排序后的字典。 我们可以使用切片来获得此排序字典中最不常见的元素。

减去()和更新() (subtract() and update())

Counter method is used to subtract element counts from another counter. method is used to add counts from another counter.

计数器方法用于从另一个计数器中减去元素计数。 方法用于添加另一个计数器的计数。

We can perform some arithmetic operations on Counters too, just like numbers. However only elements with positive count are returned with these operations.

我们也可以像数字一样在Counters上执行一些算术运算。 但是,这些操作仅返回具有正计数的元素。

Let’s look at some code snippets for miscellaneous operations we can perform on Counter objects.

让我们看一下一些可以在Counter对象上执行的其他操作的代码段。

That’s all for Python Counter class.

这就是Python Counter类的全部内容。

GitHub Repository. GitHub Repository下载完整的示例代码。

Reference: Python Docs

参考: Python文档

翻译自: https://www.journaldev.com/20806/python-counter-python-collections-counter

图木舒克版权声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕,E-mail:xinmeigg88@163.com

展开全文READ MORE
计数器手机
话费慢充93充100,这也是一门好生意移动手机充值「话费慢充93充100,这也是一门好生意」 qq手机客户端怎么安装: 详细步骤教你如何安装QQ手机客户端安装手机qq「qq手机客户端怎么安装: 详细步骤教你如何安装QQ手机客户端」