比较两个字典,并根据比较后的字典更新字典

时间:2019-04-05 10:02:21

标签: python python-2.7

比较两个字典并基于比较的字典更新字典的解决方案

比较dict1和dict2并根据dict键值更新dict1,其中“ id”是唯一键

dict1={id:1,mobile:'nokia',count:54,size:9}

dict2={id:1,mobile:'nokia',count:63,size:9}

预期输出:

dict1={id:1,mobile:'nokia',count:63,size:9}

1 个答案:

答案 0 :(得分:0)

示例代码:

dict1={"id":1,"mobile":'nokia',"count":54,"size":9}

dict2={"id":1,"mobile":'nokia',"count":63,"size":9}

dict1.update(dict2)

print(dict1)

输出:

{'id': 1, 'mobile': 'nokia', 'count': 63, 'size': 9}