I need a bag/multiset-like data type in Python. I understand collections.Counter is often used for this purpose. But the comparison operators don't seem to work:
In [1]: from collections import Counter
In [2]: bag1 = Counter(a=1, b=2, c=3)
In [3]: bag2 = Counter(a=2, b=2)
In [4]: bag1 > bag2
Out[4]: True
This seems like a bug to me. I expected the less-than and greater-than operators to perform set-like subset and superset comparisons. But if that were the case then bag1 > bag2
would be false because bag2
contains an extra 'a'
. There also don't seem to be subset/superset methods on Counter objects. So I have two questions:
- What comparison logic is used for Counter objects?
- How can I compare Counter objects for subset, superset, proper-subset, and proper-superset?