考虑两个python字典:
>>> a = {'one': 10, 'two': 10.1, 'three': {'x': '10'}}
>>> b = {'one': 20, 'two': 20.1, 'three': {'x': '20'}}显然,两个字典a == b的比较将计算为False,然而,值的数据类型都是相等的。
比较这两本字典值的数据类型的最简单方法是什么?是否有一个现有的python库来进行同样的操作。
角案例
发布于 2019-05-04 08:30:11
看看deepdiff模块。
安装with:pip install deepdiff
示例用法:
from deepdiff import DeepDiff
a = {'one': 10, 'three': {'x': '10'}, 'two': '10.1'}
b = {'one': 10, 'three': {'x': '10'}, 'two': '10.1'}
i = {'one': 10, 'two': '10.1', 'three': {'x': '10'}}
j = {'one': 20, 'two': '20.1', 'three': {'x': '20'}}
m = {'one': 10, 'three': {'x': 10}, 'two': '10.1'}
n = {'one': 20, 'two': '20.1', 'three': {'x': '20'}}
ddiff1 = DeepDiff(a, b, ignore_order=True)
ddiff2 = DeepDiff(i, j, ignore_order=True)
ddiff3 = DeepDiff(m, n, ignore_order=True)
print(f"{ddiff1}\n{'type_changes' in ddiff1}\n")
print(f"{ddiff2}\n{'type_changes' in ddiff2}\n")
print(f"{ddiff3}\n{'type_changes' in ddiff3}\n")输出: {} False {‘value_changed’:{“root‘2’”:{'new_value':'20.1','old_value':'10.1'},“root‘三”:{'new_value':'20','old_value':'10'},“root’‘one’”:{'new_value':20,{“root”_type:,'new_type':,'old_value':10,'new_value':‘20’},‘value_changed’:{“root‘’one‘”:{'new_value':20,'old_value':10},“根‘2’”:{'new_value':'20.1','old_value':‘10.1’} True
https://stackoverflow.com/questions/55980783
复制相似问题