python 提供了一种内置方法 generate_hash_from_dict 对字典进行哈希,通过比较哈希值是否相同来确定字典中数据是否发生变化,demo 如下:
from dicthash import generate_hash_from_dict
d = {'b': 0.12, 'a': 'asd', 3: {'c': [3, 4, 5]}}
e = {'b': 0.12, 'a': 'asd', 3: {'c': [3, 4, 5]}}
print(generate_hash_from_dict(d) == generate_hash_from_dict(e))
d = {'a': 'asd', 'b': 0.12, 3: {'c': [3, 4, 5]}}
e = {'b': 0.12, 'a': 'asd', 3: {'c': [3, 4, 5]}}
print(generate_hash_from_dict(d) == generate_hash_from_dict(e))
d = {'a': 'asd', 'b': 0.12, 3: {'c': [3, 4, 5, 6]}}
e = {'b': 0.12, 'a': 'asd', 3: {'c': [3, 4, 5]}}
print(generate_hash_from_dict(d) == generate_hash_from_dict(e))
打印结果如下:
True True False



