How do I add N to each counter value? - python
Is there a numpy equivalent ufunc
for a Counter object?
For example, to add N to all values ββin the 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 keys, it will certainly 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 Counter?
source to share
You can create a new counter with the same keys and increment only, and then sum with the original:
increment = Counter(dict.fromkeys(x, n))
y = x + increment
The wrong objects Counter
fit trillions of keys; if your datasets are so large, think about how to use different tools like a database.
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})
source to share
How to use collections.defaultdict
with default +N
instead?
>>> from collections import defaultdict
>>> x = defaultdict(lambda: 2) # +N = +2
>>> for key in ['foo', 'foo', 'bar', 'bar']:
... x[key] += 1
...
>>> x
defaultdict(<function <lambda> at 0x0000000002A4D128>, {'foo': 4, 'bar': 4})
source to share