gpt4 book ai didi

Python,类型错误 : unhashable type: 'list'

转载 作者:IT老高 更新时间:2023-10-28 20:31:36 25 4
gpt4 key购买 nike

我在我的程序中收到以下错误:追溯:

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'

该程序旨在创建一个工作正常的邻接列表,然后继续搜索顶点 va 和 vb 之间是否存在路径。我在 collection/defaultdict 中使用了一个列表字典来充分附加相邻的顶点。

问题出在程序末尾创建列表后的 if 子句中。我找不到正确使用带有 dict 的 if 子句来查看顶点之间是否存在有效路径的方法。另外,grafo 是一个图类。

代码如下:

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

感谢您的帮助。

最佳答案

问题是您不能使用 list 作为 dict 中的键,因为 dict 键需要是不可变的。请改用元组。

这是一个列表:

[x, y]

这是一个元组:

(x, y)

请注意,在大多数情况下,() 是可选的,因为 , 是实际定义元组的内容(只要它是不被 []{} 包围,或用作函数参数)。

您可能会发现 the section on tuples in the Python tutorial有用:

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.

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().


如果您想知道错误消息的含义,它是在提示,因为没有内置 hash function用于列表(按设计),字典实现为 hash tables .

关于Python,类型错误 : unhashable type: 'list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19371358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com