类型错误:列表对象不可哈希
totalCost = problem.getCostOfActions(self.actions)
当然可以!请把你想要翻译的内容发给我,我会帮你把它变得简单易懂。
5 个回答
1
这个错误是因为你试图把一种不能被“哈希”的类型放进集合里。
>>> s=set((1,2))
>>> a.add([3,4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
我觉得这可能也适用于你的情况。用元组替代列表:
>> a.add((3,4))
>>>
6
你可能尝试过把像列表这样的可变对象当作字典的键或者集合的成员。可变对象的特点是它们的内容可以改变,这样一来,系统就很难高效和准确地跟踪它们。因此,它们不提供一种叫做哈希的特殊属性。
15
看起来你想把一个列表当作字典的键,或者类似的操作。可是,列表是不可哈希的,所以不能用作字典的键或者集合中的元素。
另外,当这种错误发生时,Python会给你一个堆栈跟踪信息,这里面包含了文件名和行号。你可以通过这些信息找到出错的代码。
编辑 关于堆栈跟踪:
cat > script.py
foo = [1,2,3]
bar = {}
bar[foo] = "Boom"
print "Never happens"
python script.py
Traceback (most recent call last):
File "script.py", line 3, in <module> // this is the file and the line-number
bar[foo] = "Boom"
TypeError: unhashable type: 'list'