Python,TypeError:不可更改类型:“list”

2024-03-29 07:48:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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子句中。我找不到一种方法来正确使用if子句和dict来查找顶点之间是否存在有效路径。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

谢谢你的帮助。


Tags: in程序nodeforlenifrangeint
1条回答
网友
1楼 · 发布于 2024-03-29 07:48:37

问题是不能使用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实现的。

相关问题 更多 >