Python 中的散列数组 [英] Hashing arrays in Python

查看:49
本文介绍了Python 中的散列数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以散列lists?

例如,我知道元组的散列是可能的:

<预><代码>>>>哈希((1,2,3,4,5,6))-319527650

但是可以散列一个list吗?

<预><代码>>>>哈希([1,2,3,4,5,6])哈希值

可能的解决方案:

非常深入的列表散列解释,在这里.

试试看:

<预><代码>>>>哈希((1,2,3))2528502973977326415>>>哈希([1,2,3])回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不可散列的类型:列表">>>哈希(冻结集((1,2,3)))-7699079583225461316>>>散列(设置((1,2,3)))回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不可散列的类型:设置"

所以你可以得到 tuplefrozensethash 因为它们是不可变的,你不能为 list 做set 因为它们是可变的.

Is it possible to hash lists?

For example, I know that hashes of tuples are possible:

>>> hash((1,2,3,4,5,6))
-319527650

But is it possible to hash a list?

>>> hash([1,2,3,4,5,6])
hash_value

Possible Solution:

Very in depth explanation to the hashing of lists, here.

解决方案

Just try it:

>>> 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'

So you can get hash of tuple and frozenset since the are immutable, and you can't do it for list and set because they are mutable.

这篇关于Python 中的散列数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆