简体   繁体   English

为什么collections.Counter没有对称差异?

[英]Why is there no symmetric difference for collections.Counter?

So for sets you can do a symmetric difference (^) which is the equivalent of union minus intersection. 因此,对于集合,您可以做一个对称差(^),它等于并集减去交集。 Why is ^ an unsupported operand for Counter objects while union and intersection still work? 为什么在联合和交集仍然有效的情况下^是Counter对象不受支持的操作数?

For Counter objects, & and | 对于Counter对象, &| don't mean intersection and union as they do for sets ... they mean max and min . 不要像集合那样表示交集和并集... ...表示maxmin

Several mathematical operations are provided for combining Counter objects to produce multisets (counters that have counts greater than zero). 提供了一些数学运算来组合Counter对象以产生多集(counts大于零的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. 交集和并集返回相应计数的最小值和最大值。 Each operation can accept inputs with signed counts, but the output will exclude results with counts of zero or less. 每个操作都可以接受带符号计数的输入,但是输出将排除计数为零或更少的结果。

With these definitions, what would the ^ operator mean? 使用这些定义, ^运算符是什么意思?

If you want the symmetric difference of the keys, you can do c1.viewkeys() ^ c2.viewkeys() 1 如果需要键的对称差异,可以执行c1.viewkeys() ^ c2.viewkeys() 1


1 on python3.x, use .keys() instead 在python3.x上为1 ,请改用.keys()

Expanding on my comment, turns out it was discussed at time, and rejected. 扩展我的评论,结果是当时进行了讨论,然后被拒绝。

Click the link for the full message (and its thread); 单击完整消息的链接(及其主题); I'll just quote the "high order bits" of Raymond Hettinger's reply: 我只引用雷蒙德·海廷格(Raymond Hettinger)的回复的“高阶位”:

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. 我不太可能将这种[对称差异]方法添加到Counter API中,因为用例的稀有性并不能保证API的复杂性。 IMO, adding a method like this makes the class harder to learn, understand and remember. 国际海事组织(IMO)添加了这样的方法,使班级的学习,理解和记忆更加困难。 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. 我希望看到有人在ASPN Cookbook上发布一个子类,该子类添加了许多有趣的(尽管不是常见的)操作。

... ...

The Counter() class has low aspirations. Counter()类的期望值较低。 It is a dictionary that fills-in missing values with zero and is augmented by a handful of basic methods for managing the counts. 它是一本字典,用零填充缺失值,并通过一些用于管理计数的基本方法进行了扩充。

Full message here: 完整消息在这里:

https://mail.python.org/pipermail/python-list/2010-August/585040.html https://mail.python.org/pipermail/python-list/2010-August/585040.html

There's also a recipe in the ASPN Cookbook implementing __xor__ in a Counter subclass: __xor__ Cookbook中还有一个在Counter子类中实现__xor__的方法:

http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/ http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/

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

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