简体   繁体   English

collections.Counter 的按位与到底是什么?

[英]What exactly is the bitwise AND doing with collections.Counter?

There was a recent question where the, correct, answer really surprised me by using & on two Counters and then "getting it right".最近有一个问题,通过在两个计数器上使用&然后“做对了”,正确的答案真的让我感到惊讶。

From the docs :文档

Counters support rich comparison operators for equality, subset, and superset relationships: ==, !=, <, <=, >, >=.计数器支持等式、子集和超集关系的丰富比较运算符:==、!=、<、<=、>、>=。 All of those tests treat missing elements as having zero counts so that Counter(a=1) == Counter(a=1, b=0) returns true.所有这些测试都将缺失元素视为计数为零,因此 Counter(a=1) == Counter(a=1, b=0) 返回 true。

But that doesn't go into the specifics of & .但这并没有涉及&的细节。 I wrote a small test script:我写了一个小测试脚本:

from collections import Counter
from pprint import pp

cls = Counter  # `dict` fails: TypeError: unsupported operand type

o1 = cls(a=1,b=2,c=3,de=3,f=3,i1=9)
o2 = cls(a=1,b=2,c=3,de=5,f=6,i2=9)
res = o1 & o2

pp(dict(o1=o1,o2=o2,res=res))

the output is:输出是:

{'o1': Counter({'i1': 9, 'c': 3, 'de': 3, 'f': 3, 'b': 2, 'a': 1}),
 'o2': Counter({'i2': 9, 'f': 6, 'de': 5, 'c': 3, 'b': 2, 'a': 1}),
 'res': Counter({'c': 3, 'de': 3, 'f': 3, 'b': 2, 'a': 1})}

It seems to me that counter1 & counter2 means:在我看来, counter1 & counter2的意思是:

  • calculate the intersection of the keys of both.计算两者的键的交集。
  • for the values on common keys, compute the min对于公共键上的值,计算min

Am I correct?我对么? Asides from Counter , and set , do any other standard library data structures also define __and__ (the backing dunder for & , IIRC)?除了Counterset之外,任何其他标准库数据结构是否也定义__and__& ,IIRC 的后盾)?

Your understanding is pretty much correct.你的理解是非常正确的。 If you look a couple lines down from where you've quoted , you'll see an example use of & on Counter objects -- you don't need to dive into the source code to find it: 如果你从引用的地方往下看几行,你会看到一个在Counter对象上使用&的示例——你不需要深入到源代码中来找到它:

Intersection and union return the minimum and maximum of corresponding counts.交集和并集返回相应计数的最小值和最大值。 ... ...

 >>> c & d # intersection: min(c[x], d[x]) Counter({'a': 1, 'b': 1}) >>> c | d # union: max(c[x], d[x]) Counter({'a': 3, 'b': 2})

Container docs state:容器文档状态:

Several mathematical operations are provided for combining Counter objects to produce multisets (counters that have counts greater than zero).提供了几种数学运算来组合 Counter 对象以生成多重集(计数大于零的计数器)。 Addition and subtraction combine counters by adding or subtracting the counts of corresponding elements.加法和减法通过增加或减少相应元素的计数来组合计数器。 Intersection and union return the minimum and maximum of corresponding counts.交集和并集返回相应计数的最小值和最大值。 Equality and inclusion compare corresponding counts.平等和包容比较相应的计数。 Each operation can accept inputs with signed counts, but the output will exclude results with counts of zero or less.每个操作都可以接受带符号计数的输入,但输出将排除计数为零或更少的结果。

To address your question about other dict derivatives overriding & , I don't believe so.为了解决您关于其他 dict 衍生物覆盖&的问题,我不这么认为。 However, set ( docs here) uses & , |但是, set (此处的文档)使用& , | , - and ^ to implement intersection, union, difference and symmetric difference. , -^来实现交集、并集、差分和对称差分。

Per official docs :根据官方文档

 >>> c = Counter(a=3, b=1) >>> d = Counter(a=1, b=2) >>> c + d # add two counters together: c[x] + d[x] Counter({'a': 4, 'b': 3}) >>> c - d # subtract (keeping only positive counts) Counter({'a': 2}) >>> c & d # intersection: min(c[x], d[x]) Counter({'a': 1, 'b': 1}) >>> c | d # union: max(c[x], d[x]) Counter({'a': 3, 'b': 2}) >>> c == d # equality: c[x] == d[x] False >>> c <= d # inclusion: c[x] <= d[x] False

Unary operations are also defined:一元操作也被定义:

 >>> c = Counter(a=2, b=-4) >>> +c Counter({'a': 2}) >>> -c Counter({'b': 4})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM