eng*_*ree 48 python arrays hash
哈希有可能lists
吗?
例如,我知道元组的哈希是可能的:
>>> hash((1,2,3,4,5,6))
-319527650
Run Code Online (Sandbox Code Playgroud)
但是有可能哈希list
吗?
>>> hash([1,2,3,4,5,6])
hash_value
Run Code Online (Sandbox Code Playgroud)
可能解决方案
Rom*_*huk 51
就试一试吧:
>>> hash((1,2,3))
2528502973977326415
>>> hash([1,2,3])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> hash(frozenset((1,2,3)))
-7699079583225461316
>>> hash(set((1,2,3)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
Run Code Online (Sandbox Code Playgroud)
所以,你可以得到hash
的tuple
,并frozenset
因为是不可变的,你不能做到这一点list
,并set
因为他们是可变的.
Ohm*_*ess 15
如果您确实需要使用列表作为字典键,请先尝试将其转换为字符串. my_list = str(my_list)
Python不允许您将可变数据用作词典中的键,因为插入后的更改会使对象无法找到.您可以使用元组作为键.