为了缓存目的,我需要从字典中存在的GET参数生成一个缓存键。
目前,我正在使用sha1(repr(sorted(my_dict.items()))) (sha1()是一个内部使用hashlib的方便方法),但我很好奇是否有更好的方法。
为了缓存目的,我需要从字典中存在的GET参数生成一个缓存键。
目前,我正在使用sha1(repr(sorted(my_dict.items()))) (sha1()是一个内部使用hashlib的方便方法),但我很好奇是否有更好的方法。
当前回答
如果你的字典不是嵌套的,你可以用字典的项创建一个frozenset,并使用hash():
hash(frozenset(my_dict.items()))
与生成JSON字符串或字典表示相比,这需要的计算量要小得多。
更新:请参阅下面的评论,为什么这种方法可能不会产生稳定的结果。
其他回答
这不是一个通用的解决方案(即,如果你的字典不是嵌套的,它只是微不足道的工作),但由于这里没有人建议它,我认为分享它可能是有用的。
我们可以使用(第三方)不可变包创建一个字典的不可变“快照”,如下所示:
from immutables import Map
map = dict(a=1, b=2)
immap = Map(map)
hash(immap)
这似乎比原始字典的字符串化要快。
我是从一篇不错的文章中学到的。
MD5哈希
对我来说,产生最稳定结果的方法是使用md5哈希和json.stringify
from typing import Dict, Any
import hashlib
import json
def dict_hash(dictionary: Dict[str, Any]) -> str:
"""MD5 hash of a dictionary."""
dhash = hashlib.md5()
# We need to sort arguments so {'a': 1, 'b': 2} is
# the same as {'b': 2, 'a': 1}
encoded = json.dumps(dictionary, sort_keys=True).encode()
dhash.update(encoded)
return dhash.hexdigest()
这里有一个更清晰的解决方案。
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))
如果你的字典不是嵌套的,你可以用字典的项创建一个frozenset,并使用hash():
hash(frozenset(my_dict.items()))
与生成JSON字符串或字典表示相比,这需要的计算量要小得多。
更新:请参阅下面的评论,为什么这种方法可能不会产生稳定的结果。
解决这个问题的一种方法是用字典的元素创建一个元组:
hash(tuple(my_dict.items()))