-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add missing multiset predicates to collections.Counter #84932
Comments
These missing predicates have been requested a number of times: isequal()
issubset()
issuperset()
isdisjoint() |
isequal is really strange considering we're talking about Python here. Do any of other stdlib types have that method instead of just using == (which works fine even now)? I'd even spell the second and third as <= and >=, same as set does. But if we're finally going to accept that Counters are just bags (CS term for multisets), then surely .add and .remove (and maybe .discard, setting the count to 0) would be more natural additions. I can't count (pun intended) all the times I had to write that '] += 1' just to count some element. |
I would also have preferred to use the operators <, >, <=, >=, and ==. The docs in the patch explain why we can't go down this path. Also, while counters have support for multiset operations, they continue to support other use cases a well (negative counts and fractional counts). That support can't be removed without breaking existing code that relies on it. From the outset, a Counter was just a dictionary that return 0 for missing keys. Users are free to use that concept however they want. |
After more thought, I've found a way to use the rich comparisons as requested. Doing so consistently required that the __eq__ method treat missing elements as having a zero count. See attached PR. |
I'm very glad for that. :-) For the other part of my message, I never intended to remove the support for non-natural counts. I just wanted to add some more methods to the natural part of Counter's API. It already has some methods which assume natural counts: .elements(), for example. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: