我是靠谱客的博主 老实故事,最近开发中收集的这篇文章主要介绍python对象引用计数器_在Python中借助计数器对象对项目进行计数 前提 (The Premise) 柜台类-概述 (The Counter Class — Overview) 确定最频繁的项目 (Determine the Most Frequent Items) 数学运算 (Mathematical Operations) 集样操作 (Set-Like Operations) 其他功能亮点 (Highlights of Other Features) 结论 (Conclusions),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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对象引用计数器

最后

以上就是老实故事为你收集整理的python对象引用计数器_在Python中借助计数器对象对项目进行计数 前提 (The Premise) 柜台类-概述 (The Counter Class — Overview) 确定最频繁的项目 (Determine the Most Frequent Items) 数学运算 (Mathematical Operations) 集样操作 (Set-Like Operations) 其他功能亮点 (Highlights of Other Features) 结论 (Conclusions)的全部内容,希望文章能够帮你解决python对象引用计数器_在Python中借助计数器对象对项目进行计数 前提 (The Premise) 柜台类-概述 (The Counter Class — Overview) 确定最频繁的项目 (Determine the Most Frequent Items) 数学运算 (Mathematical Operations) 集样操作 (Set-Like Operations) 其他功能亮点 (Highlights of Other Features) 结论 (Conclusions)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部