For example, I have two dicts.
例如,我有两个词。
A = {'a':1, 'b':10, 'c':2}
B = {'b':3, 'c':4, 'd':10}
I want a result like this:
我想要一个这样的结果:
{'a':1, 'b': [10, 3], 'c':[2, 4], 'd':10}
If a key appears in both the dicts, I want to list of both the values.
如果两个词都出现了一个键,我想列出这两个值。
2 个解决方案
#1
4
I'd make all values lists:
我会制作所有值列表:
{k: filter(None, [A.get(k), B.get(k)]) for k in A.viewkeys() | B}
using dictionary view objects.
使用字典视图对象。
Demo:
演示:
>>> A = {'a':1, 'b':10, 'c':2}
>>> B = {'b':3, 'c':4, 'd':10}
>>> {k: filter(None, [A.get(k), B.get(k)]) for k in A.viewkeys() | B}
{'a': [1], 'c': [2, 4], 'b': [10, 3], 'd': [10]}
This at least keeps your value types consistent.
这至少可以保持您的价值类型的一致性。
To produce your output, you need to use the set intersection and symmetric differences between the two dictionaries:
要生成输出,需要使用两个字典之间的集合交集和对称差异:
dict({k: [A[k], B[k]] for k in A.viewkeys() & B},
**{k: A.get(k, B.get(k)) for k in A.viewkeys() ^ B})
Demo:
演示:
>>> dict({k: [A[k], B[k]] for k in A.viewkeys() & B},
... **{k: A.get(k, B.get(k)) for k in A.viewkeys() ^ B})
{'a': 1, 'c': [2, 4], 'b': [10, 3], 'd': 10}
In Python 3, dict.keys()
is a dictionary view, so you can just replace all .viewkeys()
calls with .keys()
to get the same functionality there.
在Python 3中,dict.keys()是一个字典视图,因此你可以用.keys()替换所有.viewkeys()调用,以获得相同的功能。
#2
3
I would second the notion of Martijn Pieters that you problably want to have the same type for all the values in your result dict.
我会想到Martijn Pieters的概念,你可能想要为结果字典中的所有值设置相同的类型。
To give a second option:
给出第二种选择:
you could also use the defaultdict to achieve your result quite intuitively.
你也可以使用defaultdict直观地实现你的结果。
a defaultdict is like a dict, but it has a default constructor that is called if the key doesn't exist yet.
defaultdict就像一个dict,但它有一个默认构造函数,如果该键尚不存在则会被调用。
so you would go:
所以你会去:
from collections import defaultdict
A = {'a':1, 'b':10, 'c':2}
B = {'b':3, 'c':4, 'd':10}
result = defaultdict(list)
for d in [A, B]:
for k, v in d.items():
result[k].append(v)
then in a later stage you still easily add more values to your result.
然后在稍后阶段,您仍然可以轻松地为结果添加更多值。
you can also switch to
你也可以切换到
defaultdict(set)
if you don't want duplicate values
如果你不想要重复的值
#1
4
I'd make all values lists:
我会制作所有值列表:
{k: filter(None, [A.get(k), B.get(k)]) for k in A.viewkeys() | B}
using dictionary view objects.
使用字典视图对象。
Demo:
演示:
>>> A = {'a':1, 'b':10, 'c':2}
>>> B = {'b':3, 'c':4, 'd':10}
>>> {k: filter(None, [A.get(k), B.get(k)]) for k in A.viewkeys() | B}
{'a': [1], 'c': [2, 4], 'b': [10, 3], 'd': [10]}
This at least keeps your value types consistent.
这至少可以保持您的价值类型的一致性。
To produce your output, you need to use the set intersection and symmetric differences between the two dictionaries:
要生成输出,需要使用两个字典之间的集合交集和对称差异:
dict({k: [A[k], B[k]] for k in A.viewkeys() & B},
**{k: A.get(k, B.get(k)) for k in A.viewkeys() ^ B})
Demo:
演示:
>>> dict({k: [A[k], B[k]] for k in A.viewkeys() & B},
... **{k: A.get(k, B.get(k)) for k in A.viewkeys() ^ B})
{'a': 1, 'c': [2, 4], 'b': [10, 3], 'd': 10}
In Python 3, dict.keys()
is a dictionary view, so you can just replace all .viewkeys()
calls with .keys()
to get the same functionality there.
在Python 3中,dict.keys()是一个字典视图,因此你可以用.keys()替换所有.viewkeys()调用,以获得相同的功能。
#2
3
I would second the notion of Martijn Pieters that you problably want to have the same type for all the values in your result dict.
我会想到Martijn Pieters的概念,你可能想要为结果字典中的所有值设置相同的类型。
To give a second option:
给出第二种选择:
you could also use the defaultdict to achieve your result quite intuitively.
你也可以使用defaultdict直观地实现你的结果。
a defaultdict is like a dict, but it has a default constructor that is called if the key doesn't exist yet.
defaultdict就像一个dict,但它有一个默认构造函数,如果该键尚不存在则会被调用。
so you would go:
所以你会去:
from collections import defaultdict
A = {'a':1, 'b':10, 'c':2}
B = {'b':3, 'c':4, 'd':10}
result = defaultdict(list)
for d in [A, B]:
for k, v in d.items():
result[k].append(v)
then in a later stage you still easily add more values to your result.
然后在稍后阶段,您仍然可以轻松地为结果添加更多值。
you can also switch to
你也可以切换到
defaultdict(set)
if you don't want duplicate values
如果你不想要重复的值