Union of many Counters

自古美人都是妖i 提交于 2019-12-02 00:06:31

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.

I think a loop is much more readable:

def counter_union(iterable):
    union = Counter()
    for counter in counters:
        union |= counter
    return union

user2357112 and Tim's comments made me realize that if you are going to use reduce, you should at least use operator.ior, not operator.or_. In Python 3.3+, this will avoid creating a new counter for every iteration.

def counter_union(iterable):
    return functools.reduce(operator.ior, iterable, Counter())
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!