为了缓存目的,我需要从字典中存在的GET参数生成一个缓存键。
目前,我正在使用sha1(repr(sorted(my_dict.items()))) (sha1()是一个内部使用hashlib的方便方法),但我很好奇是否有更好的方法。
为了缓存目的,我需要从字典中存在的GET参数生成一个缓存键。
目前,我正在使用sha1(repr(sorted(my_dict.items()))) (sha1()是一个内部使用hashlib的方便方法),但我很好奇是否有更好的方法。
当前回答
这里有一个更清晰的解决方案。
def freeze(o):
if isinstance(o,dict):
return frozenset({ k:freeze(v) for k,v in o.items()}.items())
if isinstance(o,list):
return tuple([freeze(v) for v in o])
return o
def make_hash(o):
"""
makes a hash out of anything that contains only list,dict and hashable types including string and numeric types
"""
return hash(freeze(o))
其他回答
这不是一个通用的解决方案(即,如果你的字典不是嵌套的,它只是微不足道的工作),但由于这里没有人建议它,我认为分享它可能是有用的。
我们可以使用(第三方)不可变包创建一个字典的不可变“快照”,如下所示:
from immutables import Map
map = dict(a=1, b=2)
immap = Map(map)
hash(immap)
这似乎比原始字典的字符串化要快。
我是从一篇不错的文章中学到的。
这里有一个更清晰的解决方案。
def freeze(o):
if isinstance(o,dict):
return frozenset({ k:freeze(v) for k,v in o.items()}.items())
if isinstance(o,list):
return tuple([freeze(v) for v in o])
return o
def make_hash(o):
"""
makes a hash out of anything that contains only list,dict and hashable types including string and numeric types
"""
return hash(freeze(o))
更新自2013年回复…
以上答案在我看来都不可靠。原因是使用了items()。据我所知,这是一个依赖于机器的顺序。
这个怎么样?
import hashlib
def dict_hash(the_dict, *ignore):
if ignore: # Sometimes you don't care about some items
interesting = the_dict.copy()
for item in ignore:
if item in interesting:
interesting.pop(item)
the_dict = interesting
result = hashlib.sha1(
'%s' % sorted(the_dict.items())
).hexdigest()
return result
使用DeepDiff模块中的DeepHash
from deepdiff import DeepHash
obj = {'a':'1',b:'2'}
hashes = DeepHash(obj)[obj]
如果你的字典不是嵌套的,你可以用字典的项创建一个frozenset,并使用hash():
hash(frozenset(my_dict.items()))
与生成JSON字符串或字典表示相比,这需要的计算量要小得多。
更新:请参阅下面的评论,为什么这种方法可能不会产生稳定的结果。