我想通过它们的键合并两个字典的值。例如:
d1 = {'a':1, 'b':2, 'c':3}
d2 = {'a':2, 'b':[2,3], 'd':3}
期望的输出:
{'a': [1, 2], 'b': [2, 2, 3], 'c': [3], 'd': [3]}
我目前所拥有的是
d12 = {}
for d in (d1, d2):
for k,v in d.items():
d12.setdefault(k, []).append(v)
该技术产生
d12 = {'a': [1, 2], 'b': [2, [2, 3]], 'c': [3], 'd': [3]}
不是期望的输出结果。
我在 Stack Overflow 上搜索了一下,发现这篇帖子回答了我的问题,只要它没有抛出 TypeError: can only concatenate tuple (not "int") to tuple.
的错误。