Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add N to every value of a Counter? - python

Is there any equivalent to numpy ufunc for a Counter object?

For example to add N to all values in counter, I have to do this:

>>> from collections import Counter
>>> x = Counter(['foo', 'foo', 'bar', 'bar'])
>>> n = 2
>>> y = Counter({k:v+n for k,v in x.items()})
>>> x
Counter({'foo': 2, 'bar': 2})
>>> y
Counter({'foo': 4, 'bar': 4})

Given 1 trillion key, it will surely take much longer than using numpy:

>>> import numpy as np
>>> x = Counter(['foo', 'foo', 'bar', 'bar'])
>>> k, v = zip(*x.items())
>>> n = 2
>>> _v = np.array(v) + n
>>> y = Counter(dict(zip(k, list(_v))))
>>> y
Counter({'foo': 4, 'bar': 4})

Is there any other way to achieve the same +N for all values in a Counter?

like image 893
alvas Avatar asked Nov 01 '25 15:11

alvas


1 Answers

You could create a new Counter with the same keys and only the increment then sum that with the original:

increment = Counter(dict.fromkeys(x, n))
y = x + increment

Not that Counter objects are suited for trillions of keys; if your datasets are that large consider using different tools, like a database for example.

Demo:

>>> from collections import Counter
>>> x = Counter(['foo', 'foo', 'bar', 'bar'])
>>> n = 2
>>> x + Counter(dict.fromkeys(x, n))
Counter({'foo': 4, 'bar': 4})
>>> y = Counter(['foo', 'foo', 'bar', 'bar', 'spam'])
>>> y + Counter(dict.fromkeys(y, n))
Counter({'foo': 4, 'bar': 4, 'spam': 3})
like image 112
Martijn Pieters Avatar answered Nov 04 '25 11:11

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!