python中list不能做索引


先看python中内置的list不能作为字典的key.

可将list或者ndarray转化为tuple再做索引。

list不能进行hash:

import numpy as np
a1 = np.arange(3)
a2 = np.arange(3)
hash1 = hash(a1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'numpy.ndarray'

两个ndarray转为tuple后进行hash,所得的hash值是相同的

t1 = tuple(a1) t2 = tuple(a2)
hash1 = hash(t1)
hash2 = hash(t2)
print(hash1 == hash2)
True

 


更新…

a1 = [1,2,3]
h1 = hash(str(a1))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM