Python,类型错误:不可散列的类型:“列表" [英] Python, TypeError: unhashable type: 'list'

查看:59
本文介绍了Python,类型错误:不可散列的类型:“列表"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

该程序旨在执行一个工作正常的邻接表,然后继续搜索顶点 va 和 vb 之间是否存在路径.我在 collection/defaultdict 中使用了一个列表字典,所以我可以正确地附加相邻的顶点.

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.

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

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.

代码如下:

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 键必须是不可变的.改用元组.

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.

这是一个列表:

[x, y]

这是一个元组:

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

您可能会发现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.

在关于字典的部分:

与由一系列数字索引的序列不同,字典由键索引,键可以是任何不可变类型;字符串和数字始终可以是键.如果元组仅包含字符串、数字或元组,则它们可以用作键;如果元组直接或间接包含任何可变对象,则它不能用作键.您不能将列表用作键,因为可以使用索引分配、切片分配或 append() 和 extend() 等方法就地修改列表.

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

<小时>

如果您想知道错误消息的含义,它会抱怨,因为没有内置的哈希函数列表(按设计)和字典实现为哈希表.


In case you're wondering what the error message means, it's complaining because there's no built-in hash function for lists (by design), and dictionaries are implemented as hash tables.

这篇关于Python,类型错误:不可散列的类型:“列表"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆