Python,类型错误:不可散列的类型:“列表”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19371358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python, TypeError: unhashable type: 'list'
提问by Rex
i'm reciving the following error in my program: Traceback:
我在我的程序中收到以下错误:回溯:
Traceback (most recent call last):
File "C:\Python33\Archive\PythonGrafos\Alpha.py", line 126, in <module>
menugrafos()
File "C:\Python33\Archive\PythonGrafos\Alpha.py", line 97, in menugrafos
zetta = Beta.caminhografo(grafo,va,vb)
File "C:\Python33\Archive\PythonGrafos\Beta.py", line 129, in caminhografo
if ([vo, a]) in vat == ([vo,vq]) in vat:
TypeError: unhashable type: 'list'
The program is meant to do an adjacency list which works fine and then proceed to search if there is a path between vertex va and vb. I used a dictionary of lists in collection/defaultdict so i can properly append adjacent vertex.
该程序旨在执行一个工作正常的邻接表,然后继续搜索顶点 va 和 vb 之间是否存在路径。我在 collection/defaultdict 中使用了一个列表字典,所以我可以正确地附加相邻的顶点。
The problem is in the if clauses after the list is created at the end of the program. I can't find a way to properly use the if clauses with the dict to find if there is a valid path between vertex. Also grafo is a graph class.
问题出在程序末尾创建列表之后的 if 子句中。我找不到一种方法来正确使用带有 dict 的 if 子句来查找顶点之间是否存在有效路径。grafo 也是一个图形类。
Here is the code:
这是代码:
class graph:
v = 0
a = 0
node = []
class vertex:
ta = []
adj = {}
def caminhografo(grafo, va, vb):
vat = defaultdict(list)
i = 0
a = 0
z = 0
vo = int(va)
vq = int(vb)
vz = int(va)
vw = int(vb)
x = len(grafo.node)
if vz < vw:
for vz in range (vw+1):
a = 0
x = len(grafo.node)
for a in range (x):
if [int(vz),int(a)] in grafo.node:
vat[vz].append(a)
if vz > vw:
while vz > vw:
a = 0
x = len(grafo.node)
for a in range (x):
if[int(va),int(a)] in grafo.node:
vat[vz].append(a)
vz = vz - 1
a = 0
x = len(grafo.node)
print(vat)
for a in range (x):
if ([vo, a]) in vat == ([vo,vq]) in vat:
print("""
==============================================
Existe Caminho
==============================================
""")
break
elif ([vo,a]) in vat:
vo = a
else:
print("""
==============================================
N?o Existe Caminho
==============================================
""")
break
Thanks for any assistance.
感谢您的帮助。
采纳答案by Brendan Long
The problem is that you can't use a list
as the key in a dict
, since dict
keys need to be immutable. Use a tuple instead.
问题是您不能使用 alist
作为a中的键dict
,因为dict
键必须是不可变的。改用元组。
This is a list:
这是一个列表:
[x, y]
This is a tuple:
这是一个元组:
(x, y)
Note that in most cases, the (
and )
are optional, since ,
is what actually defines a tuple (as long as it's not surrounded by []
or {}
, or used as a function argument).
请注意,在大多数情况下,(
and)
是可选的,因为,
它实际上定义了一个元组(只要它没有被[]
or包围{}
,或者用作函数参数)。
You might find the section on tuples in the Python tutorialuseful:
您可能会发现Python 教程中有关元组的部分很有用:
Though tuples may seem similar to lists, they are often used in different situations and for different purposes. Tuples are immutable, and usually contain an heterogeneous sequence of elements that are accessed via unpacking (see later in this section) or indexing (or even by attribute in the case of namedtuples). Lists are mutable, and their elements are usually homogeneous and are accessed by iterating over the list.
尽管元组看起来与列表相似,但它们通常用于不同的情况和不同的目的。元组是不可变的,通常包含通过解包(见本节后面部分)或索引(甚至在命名元组的情况下通过属性)访问的异构元素序列。列表是可变的,它们的元素通常是同构的,可以通过遍历列表来访问。
And in the section on dictionaries:
在字典部分:
Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. You can't use lists as keys, since lists can be modified in place using index assignments, slice assignments, or methods like append() and extend().
与由一系列数字索引的序列不同,字典由键索引,键可以是任何不可变类型;字符串和数字始终可以是键。如果元组仅包含字符串、数字或元组,则它们可以用作键;如果元组直接或间接包含任何可变对象,则它不能用作键。您不能将列表用作键,因为可以使用索引分配、切片分配或 append() 和 extend() 等方法就地修改列表。
In case you're wondering what the error message means, it's complaining because there's no built-in hash functionfor lists (by design), and dictionaries are implemented as hash tables.