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

Support collections.Counter ? #1663

Open
paugier opened this issue Nov 4, 2020 · 1 comment
Open

Support collections.Counter ? #1663

paugier opened this issue Nov 4, 2020 · 1 comment

Comments

@paugier
Copy link
Contributor

paugier commented Nov 4, 2020

https://docs.python.org/3.8/library/collections.html#collections.Counter can be very useful in numerical kernels.

Since they are very similar to dict, it may be not too difficult to implement, especially a simplified version without the extra methods elements, most_common, ... ?

Without Counter, one needs to replace

def main_not_supported(...):
    ...
    counter = Counter()
    print(counter["foo"])
    counter["bar"] += 1
    ...

by something like:

def increment_counter(counter, key):
    try:
        counter[key] += 1
    except KeyError:
        counter[key] = 1

def get_counter_value(counter, key):
    try:
        return counter[key]
    except KeyError:
        return 0

def main(...):
    counter = {}
    print(get_counter_value(counter, "foo"))
    increment_counter(counter, "bar")
@serge-sans-paille
Copy link
Owner

I agree this would be a valuable addition. Needs some time to do it though :-)

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

No branches or pull requests

2 participants