为什么collections.Counter没有对称差异? [英] Why is there no symmetric difference for collections.Counter?
问题描述
因此,对于集合,您可以做一个对称差(^),它等于联合减去交集.为什么在联合和交集仍然有效的情况下^是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?
推荐答案
扩大我的评论,发现当时讨论的内容被拒绝了.
Expanding on my comment, turns out it was discussed at time, and rejected.
单击完整消息的链接(及其主题);我只引用雷蒙德·海廷格(Raymond Hettinger)的回复的高阶位":
Click the link for the full message (and its thread); I'll just quote the "high order bits" of Raymond Hettinger's reply:
我不太可能将这种[对称差异]方法添加到Counter API中,因为用例的稀缺性并不能保证增加的API复杂性.国际海事组织(IMO),添加了这样的方法,使得该类学习起来更加困难,了解和记住.似乎并没有太大的胜利使用现有的替代方法:
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:
...
我希望看到有人在ASPN Cookbook上发布一个子类,添加了一些有趣的,尽管不是常见的操作.
I would like to see someone post a subclass to the ASPN Cookbook that adds a number of interesting, though not common operations.
...
Counter()类的期望值较低.这是一本字典用零填充缺失值,并用少数几个填充计数的基本方法.
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 __
的方法:
There's also a recipe in the ASPN Cookbook implementing __xor__
in a Counter
subclass:
http://code.activestate.com/recipes/577362-extension-to-python-3-counter-class/
这篇关于为什么collections.Counter没有对称差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!