Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Union of many Counters

Tags:

python

counter

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?

like image 845
flornquake Avatar asked Dec 16 '22 06:12

flornquake


1 Answers

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.

like image 186
Tim Peters Avatar answered Dec 27 '22 03:12

Tim Peters