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

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.mzph.cn/news/388015.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

套接字设置为(非)阻塞模式

当socket 进行TCP 连接的时候(也就是调用connect 时),一旦网络不通,或者是ip 地址无效,就可能使整个线程阻塞。一般为30 秒(我测的是20 秒)。如果设置为非阻塞模式,能很好的解决这个…

经典问题之「分支预测」

问题 来源 &#xff1a;stackoverflow 为什么下面代码排序后累加比不排序快&#xff1f; public static void main(String[] args) {// Generate dataint arraySize 32768;int data[] new int[arraySize];Random rnd new Random(0);for (int c 0; c < arraySize; c)data…

vi

vi filename :打开或新建文件&#xff0c;并将光标置于第一行首 vi n filename &#xff1a;打开文件&#xff0c;并将光标置于第n行首 vi filename &#xff1a;打开文件&#xff0c;并将光标置于最后一行首 vi /pattern filename&#xff1a;打开文件&#xff0c;并将光标置…

数字图像处理 python_5使用Python处理数字的高级操作

数字图像处理 pythonNumbers are everywhere in our daily life — there are phone numbers, dates of birth, ages, and other various identifiers (driver’s license and social security numbers, for example).电话号码在我们的日常生活中无处不在-电话号码&#xff0c;…

05精益敏捷项目管理——超越Scrum

00.我们不是不知道它会给我们带来麻烦&#xff0c;只是没想到麻烦会有这么多。——威尔.罗杰斯 01.知识点&#xff1a; a.Scrum是一个强大、特意设计的轻量级框架&#xff0c;器特性就是将软件开发中在制品的数量限制在团队层级&#xff0c;使团队有能力与业务落班一起有效地开…

带标题的图片轮询展示

为什么80%的码农都做不了架构师&#xff1f;>>> <div> <table width"671" cellpadding"0" cellspacing"0"> <tr height"5"> <td style"back…

linux java 查找进程中的线程

这里对linux下、sun(oracle) JDK的线程资源占用问题的查找步骤做一个小结&#xff1b;linux环境下&#xff0c;当发现java进程占用CPU资源很高&#xff0c;且又要想更进一步查出哪一个java线程占用了CPU资源时&#xff0c;按照以下步骤进行查找&#xff1a;(一)&#xff1a;通过…

定位匹配 模板匹配 地图_什么是地图匹配?

定位匹配 模板匹配 地图By Marie Douriez, James Murphy, Kerrick Staley玛丽杜里兹(Marie Douriez)&#xff0c;詹姆斯墨菲(James Murphy)&#xff0c;凯里克史塔利(Kerrick Staley) When you request a ride, Lyft tries to match you with the driver most suited for your…

Sprint计划列表

转载于:https://www.cnblogs.com/zhs20160715/p/9953586.html

MySQL学习【第十二篇事务中的锁与隔离级别】

一.事务中的锁 1.啥是锁&#xff1f; 顾名思义&#xff0c;锁就是锁定的意思 2.锁的作用是什么&#xff1f; 在事务ACID的过程中&#xff0c;‘锁’和‘隔离级别’一起来实现‘I’隔离性的作用 3.锁的种类 共享锁&#xff1a;保证在多事务工作期间&#xff0c;数据查询不会被阻…

Android WebKit

这段时间基于项目需要 在开发中与WebView的接触比较多&#xff0c;前段时间关于HTML5规范尘埃落定的消息出现在各大IT社区头版上&#xff0c;更有人说&#xff1a;HTML5将颠覆原生App开发 虽然我不太认同这一点 但是关于HTML5JSCSSNative的跨平台开发模式还是为很多企业节省了开…

jQuery的事件绑定和解绑

1、绑定事件 语法&#xff1a; bind(type,data,fn) 描述&#xff1a;为每一个匹配元素的特定事件&#xff08;像click&#xff09;绑定一个事件处理器函数。 参数解释&#xff1a; type (String) : 事件类型 data (Object) : (可选) 作为event.data属性值传递给事件对象的额外数…

软件测试框架课程考试_那考试准备课程值得吗?

软件测试框架课程考试By Levi Petty李维佩蒂(Levi Petty) This project uses a public, synthesized exam scores dataset from Kaggle to analyze average scores in Math, Reading, and Writing subject areas, relative to the student’s parents’ level of education an…

开博第一天

开博第一天 纪念一下 转载于:https://www.cnblogs.com/yang-9654/p/9959388.html

GitLab 11.9 正式发布,自动化工具 ChatOps 已开源

GitLab 11.9 已正式发布&#xff0c;该版本新增了两个和安全相关的特性&#xff0c;一是快速检查私密信息是否泄漏&#xff0c;从该版本起在 CI/CD 过程中会扫描开发者提交的信息是否包含私密内容&#xff0c;有的话会在合并 PR 时向开发者发送警报&#xff1b;二是改进了合并 …

DOCKER windows安装

DOCKER windows安装 DOCKER windows安装 1.下载程序包2. 设置环境变量3. 启动DOCKERT4. 分析start.sh5. 利用SSH工具管理6. 下载镜像 6.1 下载地址6.2 用FTP工具上传tar包6.3 安装6.4 查看镜像6.5 运行 windows必须是64位的 1.下载程序包 安装包 https://github.com/boot2doc…

为什么在Python代码中需要装饰器

Python is praised for its clarity and syntactic sugariness. In this article, I will teach you to use decorators in Python to make your code readable and clean.Python的清晰性和语法含糖度受到赞誉。 在本文中&#xff0c;我将教您在Python中使用装饰器&#xff0c;…

Elasticsearch Reference [6.7] » Modules » Network Settings

2019独角兽企业重金招聘Python工程师标准>>> Search Settings Node Network Settingsedit Elasticsearch binds to localhost only by default. This is sufficient for you to run a local development server (or even a development cluster, if you star…

【百度】大型网站的HTTPS实践(一)——HTTPS协议和原理

大型网站的HTTPS实践&#xff08;一&#xff09;——HTTPS协议和原理 原创 网络通信/物联网 作者&#xff1a;AIOps智能运维 时间&#xff1a;2018-11-09 15:07:39 349 0前言 百度于2015年上线了全站HTTPS的安全搜索&#xff0c;默认会将HTTP请求跳转成HTTPS。从今天开始&…

数据清理最终实现了自动化

苹果 | GOOGLE | 现货 | 其他 (APPLE | GOOGLE | SPOTIFY | OTHERS) Editor’s note: The Towards Data Science podcast’s “Climbing the Data Science Ladder” series is hosted by Jeremie Harris. Jeremie helps run a data science mentorship startup called Sharpest…