What's the best way (in terms of readability and efficiency) of finding the union of a list of Counters?
For example, my list might look like this:
counters = [Counter({'a': 6, 'b': 3, 'c': 1}),
Counter({'a': 2, 'b': 5}),
Counter({'a': 4, 'b': 4}),
...]
I want to calculate the union, i.e. counters[0] | counters[1] | counters[2] | ....
One way of doing it would be this:
def counter_union(iterable):
return functools.reduce(operator.or_, iterable, Counter())
Is there a better approach?
Goodness, when did Python programmers become afraid of easy loops? LOL.
result = Counter()
for c in counters:
result |= c
There really aren't prizes in real life for squashing things into as few characters as theoretically possible. Well, ya, there are in Perl, but not in Python ;-)
Later: pursuant to user2357112's comment, starting with Python 3.3 the code above will do "in place" unions into result. That is, result is truly reused, possibly growing larger on each iteration.
In any spelling of
counters[0] | counters[1] | counters[2] | ...
instead, the entire partial result so far keeps getting thrown away when the next partial result is computed. That may - or may not - be a lot slower.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With