There are two dictionaries
x={1:['a','b','c']}
y={1:['d','e','f'],2:['g']}
I want another dictionary z which is a merged one of x and y such that
z = {1:['a','b','c','d','e','f'],2:['g']}
Is it possible to do this operation?
I tried update operation
x.update(y)
But it gives me the following result
z= {1:['d','e','f'],2:['g']}
解决方案
Counter() can be used in this case:
>>> x={1:['a','b','c']}
>>> y={1:['d','e','f'],2:['g']}
>>> from collections import Counter
>>> Counter(x) + Counter(y)
Counter({2: ['g'], 1: ['a', 'b', 'c', 'd', 'e', 'f']})
If desired result is a dict. You can use the following:
z = dict(Counter(x) + Counter(y))