概述
python对象引用计数器 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 当我们在Python中处理数据容器(例如元组和列表)时,我们经常需要计算特定元素。 一种常见的实现方法是使用 Let’s take a look at some code for its use: 让我们看一些使用它的代码: As you can see above, we used the 如上所示,我们将 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: 需要注意的一件事:如果函数中指定的元素未包含在列表中,我们将得到预期的零计数。 如果要计算所有元素的出现次数,则必须对其进行迭代,如以下代码片段所示: Several things are worth highlighting in the above: 上面值得强调的几件事: To avoid counting elements of the same value, we use the 为了避免计数相同值的元素,我们使用 The 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 是否有另一种更好的方法来解决该问题? 如果您了解Python,则应该猜测答案是肯定的。 The As you can see at line seven, a 如您在第七行看到的, One critical thing to note is that because of the key-value mapping mechanism, only hashable objects can be tracked by a 需要注意的关键一点是,由于键值映射机制, The following code shows you a trivial example when we try to use 以下代码向您展示了一个简单的示例,当我们尝试将 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 通常,我们需要知道数据容器(例如列表和元组)中经常出现的项目是什么。 在看到带有 As shown above, we first needed to use the list’s 如上所示,我们首先需要使用列表的 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 如上所示,您应该获得的第一印象是将代码的简洁程度与该功能的非惯用实现相比。 这是因为 Please note that when you don’t specify any numbers in the 请注意,当您未在 Previously, we saw that the 之前,我们看到了 One possible solution is to take advantage of a 一种可能的解决方案是利用 There’s a more elegant way to update the original 有一种更优雅的方式来更新原始的 The name of the The following code shows you such operation, which should be self-explanatory: 下面的代码向您展示了这样的操作,这应该是不言而喻的: In a similar fashion, we can subtract one 以类似的方式,我们可以使用减号运算符从另一个 In the above code, one thing to note is that when we use the subtraction operator, the resulting 在上面的代码中,需要注意的一件事是,当我们使用减法运算符时,所得的 As you can see, the resulting 如您所见,生成的 In addition to the support of mathematical operations, there are two set-like operations available to the 除了支持数学运算外, To create an “intersection” of two 要创建两个 In the previous sections, we learned that we can create 在上一节中,我们了解了可以从可迭代对象(包括元组,列表和字符串)创建 We’ve seen that the counts of the 我们已经看到, Another useful method of a In this article, we reviewed the interesting 在本文中,我们回顾了有趣的 Most interestingly, it has the 最有趣的是,它具有 Thanks for reading. 谢谢阅读。 翻译自: https://medium.com/better-programming/count-items-in-python-with-the-help-of-counter-objects-c08d8d486e45 python对象引用计数器 前提 (The Premise)
count()
function — you specify the element you want to count and the function returns the count.count()
函数-您指定要计数的元素,然后该函数返回计数。 count()
function with a list of scores.count()
函数与分数列表一起使用。 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循环将仅遍历不同的元素以获得正确的累计计数。 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中的序列数据,包括字符串,列表,元组和字节。 Counter
class is specifically designed to count elements in these data structures.Counter
类专门设计用于对这些数据结构中的元素进行计数。 柜台类-概述 (The Counter Class — Overview)
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 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
对象中不存在键,则计数将为零。 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中,不可变对象(例如字符串,整数和元组)都是可哈希的,而可变对象(例如列表,集合和字典)则不可哈希。 关于对象散列性的详细讨论超出了本文的范围,并且如果您有兴趣,可以在我上一篇有关该主题的文章中找到更多信息。 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
是不可散列的对象。 确定最频繁的项目 (Determine the Most Frequent Items)
Counter
object, let’s see how to achieve this functionality using some tedious code:Counter
对象的解决方案之前,让我们看一下如何使用一些乏味的代码来实现此功能: 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
。 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_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()
方法中指定任何数字时,所有元素都将按照计数的降序作为列表返回。 降序可能非常有用,这使我们可以检索数量最少的商品,如下所示。 更新计数器对象 (Update the Counter Object)
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
对象而不创建一个新的对象,该怎么办? 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
,正如我们在访问单个项目时看到的那样。 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
对象-使用Counter
的update()
方法。 不要将此update()
方法与dict'
的update()
方法混淆,后者会更新匹配键的值。 相反, Counter
的update()
方法将在内部生成项目的计数,并使用这些计数来更新原始的Counter
对象,如下面的代码片段所示。 数学运算 (Mathematical Operations)
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
对象时,元素的计数将合并。 Counter
object from another Counter
object using the minus operator, as shown below.Counter
对象中减去一个Counter
对象,如下所示。 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
对象一起工作。 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)
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
对象还可以使用两种类似集合的运算: union
和intersection
。 要创建两个Counter
对象的“联合”,只需使用OR运算符(即,竖线)将它们连接。 联合操作将通过使用结果Counter
对象中每个匹配键的最大计数来执行。 一个简单的示例如下所示: 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
对象中每个匹配键的最小计数来执行相交操作。 一个简单的例子如下所示。 其他功能亮点 (Highlights of Other Features)
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
类可用的实例化方法。 下面显示了一些示例,并附有解释性注释: 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
对象中删除键/值对一样。 参见下面的用法: 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()
方法,该方法创建所有项目的迭代器,每个项目都具有与其计数匹配的期望出现次数。 该方法对于迭代已知数字的不同项特别方便。 结论 (Conclusions)
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_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()
方法找出最不频繁的项目,该方法以降序对项目进行排序,从而使我们能够检索最不常见项目的最后一个项目。
最后
以上就是老实故事为你收集整理的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)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复