我想附加到字典中,例如a={1:2}a.update({2:4})a.update({1,3})I would like this to give a ={1: (2,3), 2: 4}如何才能做到这一点?没有追加。更新将其覆盖为 {1: 3, 2: 4}
1 回答
吃鸡游戏
TA贡献1829条经验 获得超7个赞
你可以尝试这样的事情:
a = {1:2}
updates = [{2:4},{1:3}]
for dct in updates:
for key in dct:
if key in a:
a[key] = (a[key], dct[key])
else:
a[key] = (dct[key])
print(a)
#Gives: {1: (2, 3), 2: 4}
添加回答
举报
0/150
提交
取消