使用Python中的set和intersection方法对字典中的key/value进行比较,再使用loop语句进行value的比较,最终输出不同的key-value对。
示例代码如下:
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'a': 1, 'b': 4, 'd': 3}
# 比较字典的key
set1 = set(dict1.keys())
set2 = set(dict2.keys())
intersection = set1.intersection(set2)
# 比较字典的value
for key in intersection:
if dict1[key] != dict2[key]:
print("key: " + key + ", values are different!")