Pregunta

How to combine multiple defaultdict(Counter)?

Given that i have two defaultdict(Counter), I have tried the following, it worked but is there any other way to achieve the combination?

>>> from collections import Counter, defaultdict
>>> x = {'a':Counter(['abc','def','abc']), 'b':Counter(['ghi', 'jkl'])}
>>> y = {'a':Counter(['abc','def','mno']), 'c':Counter(['lmn', 'jkl'])}
>>> z = x+y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> z = defaultdict(Counter)
>>> for i in x:
...     z[i].update(x[i])
... 
>>> for i in y:
...     z[i].update(y[i])
... 
>>> z
defaultdict(<class 'collections.Counter'>, {'a': Counter({'abc': 3, 'def': 2, 'mno': 1}), 'c': Counter({'jkl': 1, 'lmn': 1}), 'b': Counter({'jkl': 1, 'ghi': 1})})
¿Fue útil?

Solución

This seems okay, if a little bit code-golfy:

{k:(x.get(k,Counter()) + y.get(k,Counter())) for k in (x.keys()+y.keys())}
Out[23]: 
{'a': Counter({'abc': 3, 'def': 2, 'mno': 1}),
 'b': Counter({'jkl': 1, 'ghi': 1}),
 'c': Counter({'jkl': 1, 'lmn': 1})}

If you want to stick with the defaultdict output, you could simplify things into one loop with itertools.chain:

z = defaultdict(Counter)

for k,v in chain(x.iteritems(), y.iteritems()):
    z[k].update(v)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top