Skip to content
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

lib/collections: Allow Counter()*integer. #128332

Closed
wants to merge 1 commit into from

Conversation

abhijeetbodas2001
Copy link

This isn't part of the standard multiset operations, but makes sense to be added nonetheless, I feel!

Copy link

cpython-cla-bot bot commented Dec 29, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Dec 29, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@abhijeetbodas2001
Copy link
Author

abhijeetbodas2001 commented Dec 29, 2024

IMO, the change is too small to warrant a GH issue (or a NEWS entry), but I am happy to open one if required.

@picnixz
Copy link
Contributor

picnixz commented Dec 29, 2024

Please read the devguide first: https://devguide.python.org/getting-started/pull-request-lifecycle/. This requires an issue and a prior discussion (what are the use cases?) Until this feature is accepted, this PR should be closed.

@rhettinger rhettinger self-assigned this Dec 30, 2024
@rhettinger
Copy link
Contributor

Thank you for the suggestion, but I will decline. A counter-to-integer * operation does not match the counter-to-counter + and - operations. Having unexpectedly different semantics would create unnecessary confusion. Likely, that is why "this isn't part of the standard multiset operations".

FWIW, here is an easy way to do what you want:

>>> c = Counter(a=1, b=2, c=3)
>>> Counter({k : v*2 for k, v in c.items()})
Counter({'c': 6, 'b': 4, 'a': 2})

@rhettinger rhettinger closed this Dec 30, 2024
@abhijeetbodas2001
Copy link
Author

what are the use cases?

I was (unpleasantly) surprised to find that Counter did not support multiplication with integer while solving a particular puzzle.

So the use-case isn't a "real" one, but I made this PR since this felt natural to have.

But thanks for the reviews nonetheless!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants