python比较两个字典不同

import dictdiffer

t1 = {
    "name":"jack",
    "age":25,
    "sex":"男",
    "title":"ops",
    "heigh":"180",
}

t2 = {
    "name": "lucy",
    "age": 23,
    "sex": "女",
    "title":"sale"
}

# 获取键
# 这种只能t1的值比t2的值多才有返回值
result = set(t1) - set(t2)
print(result)
# result = {'heigh'}


# 获取键值
# 这种只能t1的值比t2的值多才有返回值
value = {k: t1[k] for k in set(t1) - set(t2)}
print(value)
# result = {'heigh': '180'}


for diff in list(dictdiffer.diff(t1, t2)):
    print(diff)

# 结果
#('change', 'name', ('jack', 'lucy'))   # 值不同
#('change', 'age', (25, 23))            # 值不同
#('change', 'sex', ('男', '女'))        # 值不同
#('change', 'title', ('ops', 'sale'))   # 值不同
#('remove', '', [('heigh', '180')])     # 少了键值对

  

posted @ 2022-12-01 11:55  lucky_tomato  阅读(382)  评论(0编辑  收藏  举报