python 把一个字典key的值加到另一个字典的key的值中? 100
字典1:{A:[2136,1888,2102,1911,1966],B:[123,698,2564]}字典2:{A:[2136,1695,2102,1888]}j结果:{...
字典1: {A:[2136, 1888, 2102, 1911, 1966],B:[123,698,2564]}
字典2: {A:[2136, 1695, 2102, 1888]}
j结果:{A:[2136, 1888, 2102, 1911, 1966,2136, 1695, 2102, 1888],B:[123,698,2564]} 展开
字典2: {A:[2136, 1695, 2102, 1888]}
j结果:{A:[2136, 1888, 2102, 1911, 1966,2136, 1695, 2102, 1888],B:[123,698,2564]} 展开
2个回答
展开全部
A = 'A'
B = 'B'
d1 = {A: [2136, 1888, 2102, 1911, 1966], B: [123, 698, 2564]}
d2 = {A: [2136, 1695, 2102, 1888]}
from collections import defaultdict
d3 = defaultdict(list)
for d in [d1, d2]:
for k in d.keys():
d3[k].extend(d[k])
print(d3)
#defaultdict(<class 'list'>, {'A': [2136, 1888, 2102, 1911, 1966, 2136, 1695, 2102, 1888], 'B': [123, 698, 2564]})
B = 'B'
d1 = {A: [2136, 1888, 2102, 1911, 1966], B: [123, 698, 2564]}
d2 = {A: [2136, 1695, 2102, 1888]}
from collections import defaultdict
d3 = defaultdict(list)
for d in [d1, d2]:
for k in d.keys():
d3[k].extend(d[k])
print(d3)
#defaultdict(<class 'list'>, {'A': [2136, 1888, 2102, 1911, 1966, 2136, 1695, 2102, 1888], 'B': [123, 698, 2564]})
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询