如何在未知长度的计数器列表中使用并集和交集?

How do I use the union and intersection on a list of Counters of unknown length?

鉴于我知道有多少个计数器,可以很容易地硬核 | 运算符以获得计数器的并集,例如:

>>> from collections import Counter
>>> x = Counter([('a'), ('a', 'bc'), ('a', 'bc'), ('xyz', 'hooli')])
>>> y = Counter([('a'), ('a'), ('a'), ('asd', 'asd')])
>>> z = Counter([('a'), ('a'), ('a'), ('a'), ('a', 'bc')])
>>> x | y | z
Counter({'a': 4, ('a', 'bc'): 2, ('asd', 'asd'): 1, ('xyz', 'hooli'): 1})

但是如果我有一个计数器列表作为函数的输入,我如何获得并集?

我是否必须遍历每个计数器然后执行并集?例如

>>> xyz = [x,y,z]
>>> def counter_union(counters):
...     union = Counter()
...     for c in counters:
...             union = union | c
...     return union
... 
>>> counter_union(xyz)
Counter({'a': 4, ('a', 'bc'): 2, ('asd', 'asd'): 1, ('xyz', 'hooli'): 1})

还有其他方法吗?

您可以使用 reduce:

>>> from functools import reduce
>>> from operator import or_
>>> reduce(or_, [x, y, z])
Counter({'a': 4, ('a', 'bc'): 2, ('xyz', 'hooli'): 1, ('asd', 'asd'): 1})

或者如果您不想导入 or_:

>>> reduce(lambda a,b: a|b, [x, y, z])
Counter({'a': 4, ('a', 'bc'): 2, ('xyz', 'hooli'): 1, ('asd', 'asd'): 1})

(与 and_/& 类似。)