为什么 collections.Counter 没有对称差异?
Why is there no symmetric difference for collections.Counter?
因此,对于集合,您可以进行对称差分 (^),这相当于并减交集。为什么 ^ 是 Counter 对象不受支持的操作数,而 union 和 intersection 仍然有效?
对于 Counter 对象,&
和 |
不像集合那样表示交集和并集...它们表示 max
和 min
。
Several mathematical operations are provided for combining Counter objects to produce multisets (counters that have counts greater than zero). 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. Each operation can accept inputs with signed counts, but the output will exclude results with counts of zero or less.
根据这些定义,^
运算符是什么意思?
如果你想要密钥的对称差异,你可以这样做c1.viewkeys() ^ c2.viewkeys()
1
1在 python3.x 上,使用 .keys()
代替
扩展我的评论,原来它曾被讨论过,但被拒绝了。
点击 link 查看完整消息(及其主题);我只引用 Raymond Hettinger 回复的"high order bits":
It's unlikely that I will add this [symmetric difference] method to the Counter API because
the rarity of use case does not warrant the added API complexity.
IMO, adding a method like this makes the class harder to learn,
understand and remember. It doesn't seem like much of a win over
using the existing alternatives:
...
I would like to see someone post a subclass to the ASPN Cookbook that
adds a number of interesting, though not common operations.
...
The Counter() class has low aspirations. It is a dictionary that
fills-in missing values with zero and is augmented by a handful of
basic methods for managing the counts.
完整消息在这里:
https://mail.python.org/pipermail/python-list/2010-August/585040.html
ASPN Cookbook 中还有一个在 Counter
子类中实现 __xor__
的方法:
http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/
因此,对于集合,您可以进行对称差分 (^),这相当于并减交集。为什么 ^ 是 Counter 对象不受支持的操作数,而 union 和 intersection 仍然有效?
对于 Counter 对象,&
和 |
不像集合那样表示交集和并集...它们表示 max
和 min
。
Several mathematical operations are provided for combining Counter objects to produce multisets (counters that have counts greater than zero). 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. Each operation can accept inputs with signed counts, but the output will exclude results with counts of zero or less.
根据这些定义,^
运算符是什么意思?
如果你想要密钥的对称差异,你可以这样做c1.viewkeys() ^ c2.viewkeys()
1
1在 python3.x 上,使用 .keys()
代替
扩展我的评论,原来它曾被讨论过,但被拒绝了。
点击 link 查看完整消息(及其主题);我只引用 Raymond Hettinger 回复的"high order bits":
It's unlikely that I will add this [symmetric difference] method to the Counter API because the rarity of use case does not warrant the added API complexity. IMO, adding a method like this makes the class harder to learn, understand and remember. It doesn't seem like much of a win over using the existing alternatives:
...
I would like to see someone post a subclass to the ASPN Cookbook that adds a number of interesting, though not common operations.
...
The Counter() class has low aspirations. It is a dictionary that fills-in missing values with zero and is augmented by a handful of basic methods for managing the counts.
完整消息在这里:
https://mail.python.org/pipermail/python-list/2010-August/585040.html
ASPN Cookbook 中还有一个在 Counter
子类中实现 __xor__
的方法:
http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/