python对象引用计数器

前提 (The Premise)

When we deal with data containers, such as tuples and lists, in Python we often need to count particular elements. One common way to do this is to use the count() function — you specify the element you want to count and the function returns the count.

当我们在Python中处理数据容器(例如元组和列表)时,我们经常需要计算特定元素。 一种常见的实现方法是使用count()函数-您指定要计数的元素,然后该函数返回计数。

Let’s take a look at some code for its use:

让我们看一些使用它的代码:

The count() function
count()函数

As you can see above, we used the count() function with a list of scores.

如上所示,我们将count()函数与分数列表一起使用。

One thing to note: When the elements specified in the function aren’t included in the list, we’ll get a count of zero, as expected. If we want to count the occurrences of all the elements, we’ll have to iterate them, as shown in the following code snippet:

需要注意的一件事:如果函数中指定的元素未包含在列表中,我们将得到预期的零计数。 如果要计算所有元素的出现次数,则必须对其进行迭代,如以下代码片段所示:

Count All Elements
计算所有元素

Several things are worth highlighting in the above:

上面值得强调的几件事:

  • To avoid counting elements of the same value, we use the set() constructor to convert these iterables to set objects. This means duplicate elements are removed — the for loop will only go over distinct elements to get their correct cumulative counts.

    为了避免计数相同值的元素,我们使用set()构造函数将这些可迭代对象转换为set对象。 这意味着删除了重复的元素-for循环将仅遍历不同的元素以获得正确的累计计数。

  • The count() function doesn’t only work with the list objects, it can also with tuples and strings. More generally, the count() function works with sequence data in Python, including strings, lists, tuples, and bytes.

    count()函数不仅适用于列表对象,还适用于元组和字符串。 更一般而言, count()函数可用于Python中的序列数据,包括字符串,列表,元组和字节。

As shown above, we have to use a for loop to iterate the elements to retrieve the counts for each individual element. It’s a bit tedious.

如上所示,我们必须使用for循环来迭代元素以检索每个单独元素的计数。 这有点乏味。

Is there another, better way to solve the problem? If you know Python, you should guess that the answer is yes. The Counter class is specifically designed to count elements in these data structures.

是否有另一种更好的方法来解决该问题? 如果您了解Python,则应该猜测答案是肯定的。 Counter类专门设计用于对这些数据结构中的元素进行计数。

柜台类-概述 (The Counter Class — Overview)

The Counter class is available through the collections module as part of Python’s standard library. As a subclass of the dict class, it provides a few highly specialized methods that handle object counting. To create a counter object, you can simply set an iterable to the Counter class instance constructor, as shown:

Counter类可作为Python标准库的一部分通过collections模块获得。 作为dict类的子类,它提供了一些处理对象计数的高度专业化的方法。 要创建一个计数器对象,您可以简单地将一个Iterable设置为Counter类实例构造函数,如下所示:

Counter Object Creation
计数器对象创建

As you can see at line seven, a Counter object looks like a dictionary with a series of key-value pairs. Specifically, the keys are the counted elements, while the values are the counters for the corresponding keys. If you want to retrieve the counts for individual items, you can do that just as you work with a dictionary. Some trivial examples are shown below. Notably, if the key doesn’t exist in the Counter object, the count will be zero.

如您在第七行看到的, Counter对象看起来像是带有一系列键值对的字典。 具体来说,键是被计数的元素,而值是对应键的计数器。 如果要检索单个项目的计数,则可以像处理字典一样进行。 一些简单的示例如下所示。 值得注意的是,如果Counter对象中不存在键,则计数将为零。

Access Individual Items
访问单个项目

One critical thing to note is that because of the key-value mapping mechanism, only hashable objects can be tracked by a Counter object. In Python, immutable objects, such as strings, integers, and tuples are all hashable, while mutable objects, such as lists, sets, and dictionaries are unhashable. A detailed discussion of object hashability is beyond the scope of the present article, and if you’re interested, you can find more information in my previous article on this topic.

需要注意的关键一点是,由于键值映射机制, Counter对象只能跟踪可哈希对象。 在Python中,不可变对象(例如字符串,整数和元组)都是可哈希的,而可变对象(例如列表,集合和字典)则不可哈希。 关于对象散列性的详细讨论超出了本文的范围,并且如果您有兴趣,可以在我上一篇有关该主题的文章中找到更多信息。

The following code shows you a trivial example when we try to use Counter with unhashable objects — lists. The error message clearly tells us that Python cannot instantiate a Counter object for us because lists are unhashable objects in Python.

以下代码向您展示了一个简单的示例,当我们尝试将Counter与不可哈希对象一起使用时- lists 。 该错误消息清楚地告诉我们,Python无法为我们实例化Counter对象,因为lists在Python lists是不可散列的对象。

Unhashable — Counter
无法散列-计数器

确定最频繁的项目 (Determine the Most Frequent Items)

Often, we need to know what the frequently occurring items in a data container (e.g., lists and tuples) are. Before we see the solution with the Counter object, let’s see how to achieve this functionality using some tedious code:

通常,我们需要知道数据容器(例如列表和元组)中经常出现的项目是什么。 在看到带有Counter对象的解决方案之前,让我们看一下如何使用一些乏味的代码来实现此功能:

The Non-Idiomatic Way
非惯用方式

As shown above, we first needed to use the list’s count() method to count each of the unique items in the list and save the counting result to a dictionary. Next, we had to use the built-in max() function to sort the items of the dictionary by specifying the sorting key to use the value of each key-value pair. Certainly, this way works but it’s not the idiomatic way, in which case, we should consider using Counter.

如上所示,我们首先需要使用列表的count()方法对列表中的每个唯一项进行计数,并将计数结果保存到字典中。 接下来,我们必须使用内置的max()函数通过指定排序键以使用每个键值对的值来对字典中的项进行排序。 当然,这种方法可行,但不是惯用的方法,在这种情况下,我们应该考虑使用Counter

Most Frequent Item
最常出现的项目

As shown above, the first impression that you should have is how concise the code is compared to the non-idiomatic implementation of this functionality. It’s all because the Counter object has a handy most_common() method, which quickly pulls the needed information for us — the most frequently occurring item and its associated count. Actually, to give us more flexibility, we have the option to find out an arbitrary number of the most frequently occurring items, as shown below.

如上所示,您应该获得的第一印象是将代码的简洁程度与该功能的非惯用实现相比。 这是因为Counter对象有一个方便的most_common()方法,该方法可以为我们快速获取所需的信息-最频繁出现的项目及其相关计数。 实际上,为给我们更大的灵活性,我们可以选择找出任意数量的最频繁出现的项目,如下所示。

Most Frequent Items
最常见的物品

Please note that when you don’t specify any numbers in the most_common() method, all the elements will be returned as a list in the descending order of the counts. This descending order can be very useful, which allows us to retrieve the item with the least count, as shown below.

请注意,当您未在most_common()方法中指定任何数字时,所有元素都将按照计数的降序作为列表返回。 降序可能非常有用,这使我们可以检索数量最少的商品,如下所示。

Least Frequent Item
最少的频繁项目

更新计数器对象 (Update the Counter Object)

Previously, we saw that the Counter object is used to count the elements in a list. But what if we get another list and want to update the original Counter object without creating a new one?

之前,我们看到了Counter对象用于对列表中的元素进行计数。 但是,如果我们得到另一个列表并想要更新原始的Counter对象而不创建一个新的对象,该怎么办?

One possible solution is to take advantage of a Counter object, implementing the mapping protocol such that we can update each key’s value accordingly. Notably, if the key doesn’t exist in the original Counter object, unlike the built-in dict data type, a KeyValue exception won’t be raised, because by default, a missing key in a Counter object has a value of 0, as we saw with accessing individual items.

一种可能的解决方案是利用Counter对象,实现映射协议,以便我们可以相应地更新每个键的值。 值得注意的是,如果键不存在于原始Counter对象中(与内置dict数据类型不同),则不会引发KeyValue异常,因为默认情况下, Counter对象中缺少的键的值为0 ,正如我们在访问单个项目时看到的那样。

Update Counter Object
更新计数器对象

There’s a more elegant way to update the original Counter object — use the Counter’s update() method. Don’t confuse this update() method with the dict’s update() method, which updates the values of matching keys. Instead, the Counter’s update() method will internally generate the counts for the items and use these counts to update the original Counter object, as shown in the code snippet below.

有一种更优雅的方式来更新原始的Counter对象-使用Counterupdate()方法。 不要将此update()方法与dict'update()方法混淆,后者会更新匹配键的值。 相反, Counterupdate()方法将在内部生成项目的计数,并使用这些计数来更新原始的Counter对象,如下面的代码片段所示。

The update() Method
update()方法

数学运算 (Mathematical Operations)

The name of the Counter class is informational, which tells you that it counts for us. As we’ve seen, we can update the counts with the update() method. More broadly, when we have multiple Counter objects, we can have addition and subtraction operations with them. When we add Counter objects, the counts for the elements are merged.

Counter类的名称是参考性的,它告诉您它对我们很重要。 如我们所见,我们可以使用update()方法更新计数。 更广泛地说,当我们有多个Counter对象时,我们可以对其进行加减运算。 当我们添加Counter对象时,元素的计数将合并。

The following code shows you such operation, which should be self-explanatory:

下面的代码向您展示了这样的操作,这应该是不言而喻的:

Addition of Counter Objects
添加计数器对象

In a similar fashion, we can subtract one Counter object from another Counter object using the minus operator, as shown below.

以类似的方式,我们可以使用减号运算符从另一个Counter对象中减去一个Counter对象,如下所示。

Subtraction of Counter Objects
相减对象

In the above code, one thing to note is that when we use the subtraction operator, the resulting Counter object will only include those key-value pairs with positive counts. This behavior is different from Counter’s method subtract(), which will also include negative counts. Also, as noted previously, when a Counter doesn’t contain the elements, they have a count of zero by default. With these two things in mind, let’s see how the subtract() method works with Counter objects.

在上面的代码中,需要注意的一件事是,当我们使用减法运算符时,所得的Counter对象将仅包括具有正计数的那些键值对。 此行为不同于Counter的方法subtract() ,该方法还将包括负数。 另外,如前所述,当Counter不包含元素时,默认情况下它们的计数为零。 牢记这两点,让我们看一下subtract()方法如何与Counter对象一起工作。

The subtract() Method
减去()方法

As you can see, the resulting Counter object includes several elements with negative counts. Another thing to note is that the subtract() method changes the counts in-place, which means that the subtract() method returns None and it changes the original Counter object’s counts.

如您所见,生成的Counter对象包含多个带有负计数的元素。 还要注意的另一件事是subtract()方法就地更改了计数,这意味着subtract()方法将返回None并且它会更改原始Counter对象的计数。

集样操作 (Set-Like Operations)

In addition to the support of mathematical operations, there are two set-like operations available to the Counter objects: union and intersection. To create a “union” of two Counter objects, you simply use the OR operator (i.e., the vertical bar) to connect them. The union operation will be carried out by using the maximal counts of each matching key in the resulting Counter object. A trivial example is shown below:

除了支持数学运算外, Counter对象还可以使用两种类似集合的运算: unionintersection 。 要创建两个Counter对象的“联合”,只需使用OR运算符(即,竖线)将它们连接。 联合操作将通过使用结果Counter对象中每个匹配键的最大计数来执行。 一个简单的示例如下所示:

Union of Counters
柜台联盟

To create an “intersection” of two Counter objects, you’ll simply use the AND operator (i.e. the & sign) to connect them. The intersection operation will be carried out by using the minimal counts of each matching key in the resulting Counter object. A trivial example is shown below.

要创建两个Counter对象的“交集”,只需使用AND运算符(即&符号)将它们连接起来。 将通过使用结果Counter对象中每个匹配键的最小计数来执行相交操作。 一个简单的例子如下所示。

Intersection of Counters
柜台相交

其他功能亮点 (Highlights of Other Features)

In the previous sections, we learned that we can create Counter objects from iterables, including tuples, lists, and strings. As mentioned previously, the Counter class is a subclass of the built-in dict class, so we can use instantiation methods that are available to the dict class. Some examples are shown below with accompanying explanatory comments:

在上一节中,我们了解了可以从可迭代对象(包括元组,列表和字符串)创建Counter对象。 如前所述, Counter类是内置dict类的子类,因此我们可以使用dict类可用的实例化方法。 下面显示了一些示例,并附有解释性注释:

Construct Counters
建造柜台

We’ve seen that the counts of the Counter objects are derived from counting or mathematical operations. We can directly set the count of particular elements if it’s an applicable usage. In addition, we can remove the elements from the Counter object, just like removing a key-value pair from a dict object. See below for such usages:

我们已经看到, Counter对象的计数是从计数或数学运算得出的。 如果可以使用的话,我们可以直接设置特定元素的数量。 另外,我们可以从Counter对象中删除元素,就像从dict对象中删除键/值对一样。 参见下面的用法:

Manipulate Counter Objects
操作计数器对象

Another useful method of a Counter object is the elements() method, which creates an iterator of all items with each having the desired occurrences matching its count. This method is particularly handy for iterating different items of known numbers.

Counter对象的另一个有用的方法是elements()方法,该方法创建所有项目的迭代器,每个项目都具有与其计数匹配的期望出现次数。 该方法对于迭代已知数字的不同项特别方便。

The elements() Method
elements()方法

结论 (Conclusions)

In this article, we reviewed the interesting Counter class for counting hashable objects in sequences, including tuples, lists, and strings. As a subclass of the dict data type, we can create Counter objects from the sequences as well as mapping and keyword arguments, just as regular dict objects. The Counter object is highly flexible in that you can manipulate the counts of these elements as you want. You can even update, merge, and intersect Counter objects.

在本文中,我们回顾了有趣的Counter类,用于对序列中的可哈希对象进行计数,包括元组,列表和字符串。 作为dict数据类型的子类,我们可以像常规dict对象一样,从序列以及映射和关键字参数中创建Counter对象。 Counter对象具有高度的灵活性,因为您可以根据需要操纵这些元素的计数。 您甚至可以更新,合并和相交Counter对象。

Most interestingly, it has the most_common() method, which allows us to find out the most frequent items with one line of code. It’s also possible to find out the least frequent items by taking advantage of the most_common() method, which sorts the items in descending order, allowing us to retrieve the last item for the least common item.

最有趣的是,它具有most_common()方法,该方法使我们可以用一行代码找出最频繁的项目。 也可以利用most_common()方法找出最不频繁的项目,该方法以降序对项目进行排序,从而使我们能够检索最不常见项目的最后一个项目。

Thanks for reading.

谢谢阅读。

翻译自: https://medium.com/better-programming/count-items-in-python-with-the-help-of-counter-objects-c08d8d486e45

python对象引用计数器


http://www.taodudu.cc/news/show-994820.html

相关文章:

  • 数字图像处理 python_5使用Python处理数字的高级操作
  • 软件测试框架课程考试_那考试准备课程值得吗?
  • 为什么在Python代码中需要装饰器
  • 数据清理最终实现了自动化
  • Python气流介绍
  • 正确的词典访问方式
  • 废水处理计算书 excel_废水监测数据是匿名的吗?
  • 数据科学还是计算机科学_您应该拥有数据科学博客的3个原因
  • 熊猫分发_流利的熊猫
  • python记录日志_5分钟内解释日志记录—使用Python演练
  • p值 t值 统计_非统计师的P值
  • 如何不部署Keras / TensorFlow模型
  • 对食材的敬畏之心极致产品_这些数据科学产品组合将给您带来敬畏和启发(2020年中的版本)
  • 向量积判断优劣弧_判断经验论文优劣的10条诫命
  • sql如何处理null值_如何正确处理SQL中的NULL值
  • 数据可视化 信息可视化_动机可视化
  • 快速数据库框架_快速学习新的数据科学概念的框架
  • 停止使用p = 0.05
  • 成像数据更好的展示_为什么更多的数据并不总是更好
  • vue domo网站_DOMO与Tableau-逐轮
  • 每个人都应该使用的Python 3中被忽略的3个功能
  • 数据探查_数据科学家,开始使用探查器
  • 从ncbi下载数据_如何从NCBI下载所有细菌组件
  • 线性插值插值_揭秘插值搜索
  • 如果您不将Docker用于数据科学项目,那么您将生活在1985年
  • docker部署flask_使用Docker,GCP Cloud Run和Flask部署Scikit-Learn NLP模型
  • 问卷 假设检验 t检验_真实问题的假设检验
  • 大数据技术 学习之旅_为什么聚焦是您数据科学之旅的关键
  • 无监督学习 k-means_无监督学习-第4部分
  • 深度学习算法原理_用于对象检测的深度学习算法的基本原理

python对象引用计数器_在Python中借助计数器对象对项目进行计数相关推荐

  1. python独立图形_在networkx中查找图形对象中的独立图形

    如果图的部分是真正不相交的(根据您的小示例),那么考虑使用^{}提取子图. 这只适用于无向图,因此如果您使用的是有向图,则需要先转换为无向图.import networkx as nx G = nx. ...

  2. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  3. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  4. python循环计数器_简单Python循环计数器issu

    欢迎来到Python!在 Python的一个优点是,绝大多数Python程序员尽可能用最"Python"的方式来做事情,所以我要做的第一步是根据PEP-8 standards重命名 ...

  5. 3_1符合python语言变量_以下选项中符合Python语言变量命名规则的是

    [单选题]以下选项中,不是Python语言特点的是 [单选题]较小的尺寸应离轮廓线较近,较大的尺寸线离轮廓线较远.( ) [单选题]关于Python语言的变量,以下选项中说法正确的是 [单选题]182 ...

  6. python创建列向量_关于Numpy中的行向量和列向量详解

    关于Numpy中的行向量和列向量详解 行向量 方式1 import numpy as np b=np.array([1,2,3]).reshape((1,-1)) print(b,b.shape) 结 ...

  7. python selenium 下拉列表_从下拉列表中选择python selenium选项

    我试图使用下面的代码使用selenium(python)从网页上的下拉列表中选择一个选项.在from selenium import webdriver from selenium.webdriver ...

  8. python 语料标注_在python中怎么标记文本?

    要运行下面的python程序,必须在系统中安装(NLTK)自然语言工具包. NLTK模块是一个庞大的工具包,旨在帮助您使用整个自然语言处理(NLP)方法. 要安装NLTK,请在终端中运行以下命令.su ...

  9. python整数池_【Python】Python中神奇的小整数对象池和大整数对象池

    小整数对象池 整数在程序中的使用非常广泛,Python为了优化速度,使用了小整数对象池, 避免为整数频繁申请和销毁内存空间. Python 对小整数的定义是 [-5, 256] 这些整数对象是提前建立 ...

最新文章

  1. MySQL 学习笔记(2)— 通配符/正则表达/运算符
  2. Exchange企业实战技巧(3)配置Exchange证书
  3. monkey学习总结笔记
  4. 字符设备驱动程序之异步通知
  5. 【算法】ROI Align 原理
  6. Oracle具有开放性吗,Oracle数据库的特点与工作原理
  7. 【ArcGIS遇上Python】Python使用栅格数据
  8. C 如何对指针进行指定字节的偏置操作
  9. linux 终端与shell输出大小不匹配
  10. 样条表示---插值和逼近样条
  11. 启动zabbix server服务
  12. 杭电计算机2011年硕士研究生笔试详解
  13. Vlookup实现多条件匹配
  14. 镇楼篇--转行初入IT的心路历程
  15. 上门洗车APP --- Android客户端开发 之 项目结构介绍
  16. BUG生命周期和管理
  17. java实现word(docx)在线编辑(word转html,html转word)——解读document.xml结构
  18. linux pki目录,Linux目录结构和常用命令
  19. 网络监控系统的建立及部署
  20. SIM 卡获取运营商信息

热门文章

  1. LeetCode【3--无重复的最长字串】 LeetCode【4--有序数组中的中位数】
  2. Linux信号实现精确到微秒的sleep函数:通过sigsuspend函数解决时序竞态问题
  3. Ubuntu环境搭建
  4. 洛谷P1080-国王游戏-贪心+高精度
  5. 生产者-消费者模型的两种实现方式
  6. Java开发热门前沿知识!成功从小公司跳槽进蚂蚁定级P6
  7. shutil模块、json和pickle模块
  8. 关于gitgithub的操作
  9. 行为扩展以及插件机制
  10. VC连接mysql数据库错误:libmysql.lib : fatal error LNK1113: invalid machine 解决方法