添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
diff = dict1.keys() & dict2 diff_vals = [(k, dict1[k], dict2[k]) for k in diff if dict1[k] != dict2[k]] print(diff_vals) #相同key,不同value #输出:[('c', 3, 5)]

其他参考:

a = {
    "x":1,
    "y":2,
    "z":3
b = {
    "x":1,
    "w":11,
    "z":12
print(a.items())
>>>dict_items([('x', 1), ('y', 2), ('z', 3)])
# 查看两个字典共有的key
print(a.keys() & b.keys())
>>>{'x', 'z'}
# 查看字典a 和字典b 的不共有的key
print(a.keys() ^  b.keys())
>>>{'y'}
# 查看在字典a里面而不在字典b里面的key
print(a.keys() - b.keys())
>>>{('x', 1)}
# 查看字典a和字典b相同的键值对
print(a.items() & b.items())
>>>{'w', 'y'}