类型错误:不可散列的类型 [英] TypeError : Unhashable type
问题描述
我正在尝试获取元组列表:类似于 [ [[(1,0),(2,0),(3,0)],[(1,1),(2,1),(3,1)....]]
我用了这个语句
set([(a,b)for a in range(3)]for b in range(3))
但它给了我一个错误
TypeError: unhashable type: 'list'
我有两个问题要问 Python 大师:
a) 当我查看 Hashable 的 Python 定义时 -
<块引用>如果一个对象的哈希值在其生命周期内永远不会改变(它需要一个 hash() 方法),那么它就是可哈希的"
当我在上面的表达式中使用 dir 函数时
dir([(a,b)for a in range(3)]for b in range(3))
似乎说 __hash__
就在那里.那么,为什么我会收到错误消息?
我能够得到 [[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]
通过使用列表命令:
list(list((a,b) for a in range(3)) for bin range(3))
b)list 和 set 都以 Iterable 作为参数.为什么一个有效(列表)而另一个无效(设置)?
您正在通过 set(...)
调用和 set 创建一个
set
需要可散列的项目.你不能有一组列表.因为列表不可哈希.
[[(a,b) for a in range(3)] for b in range(3)]
是一个列表.它不是可散列的类型.你在 dir(...) 中看到的 __hash__
不是方法,它只是 None.
列表推导式返回一个列表,您不需要在那里显式使用列表,只需使用:
<预><代码>>>>[[(a,b) for a in range(3)] for b in range(3)][[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1), 2), (2, 2)]]试试这些:
<预><代码>>>>a = {1, 2, 3}>>>b= [1, 2, 3]>>>类型(一)<类设置">>>>类型(b)<类'列表'>>>>{1, 2, []}回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不可散列的类型:列表">>>打印([].__哈希__)没有任何>>>[[],[],[]] #列表列表[[], [], []]>>>{[], [], []} #列表集回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中类型错误:不可散列的类型:列表"I am trying to get a list of list of tuples : something like [ [(1,0),(2,0),(3,0)],[(1,1),(2,1),(3,1)....]]
I used this statement
set([(a,b)for a in range(3)]for b in range(3))
But it gives me an error
TypeError: unhashable type: 'list'
I have 2 questions for the Python Guru's:
a) When I look at the Python definition of Hashable -
"An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash() method)"
when I used dir function on the expression above
dir([(a,b)for a in range(3)]for b in range(3))
it seems to say the __hash__
is there. So, why do I get the error?
I was able to get [[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]
by using the list command :
list(list((a,b) for a in range(3)) for bin range(3))
b)list and set both takes Iterable as parameter. How come one works(list) and another doesn't (set)?
You are creating a set
via set(...)
call, and set
needs hashable items. You can't have set of lists. Because list's arent hashable.
[[(a,b) for a in range(3)] for b in range(3)]
is a list. It's not a hashable type. The __hash__
you saw in dir(...) isn't a method, it's just None.
A list comprehension returns a list, you don't need to explicitly use list there, just use:
>>> [[(a,b) for a in range(3)] for b in range(3)]
[[(0, 0), (1, 0), (2, 0)], [(0, 1), (1, 1), (2, 1)], [(0, 2), (1, 2), (2, 2)]]
Try those:
>>> a = {1, 2, 3}
>>> b= [1, 2, 3]
>>> type(a)
<class 'set'>
>>> type(b)
<class 'list'>
>>> {1, 2, []}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> print([].__hash__)
None
>>> [[],[],[]] #list of lists
[[], [], []]
>>> {[], [], []} #set of lists
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
这篇关于类型错误:不可散列的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!