如何解决TypeError: unhashable type: 'list'错误

170
我正在尝试处理一个看起来像这样的文件:
AAA x 111
AAB x 111
AAA x 112
AAC x 123
...

使用字典,以便输出的结果看起来像这样。
{AAA: ['111', '112'], AAB: ['111'], AAC: [123], ...}

这是我尝试过的内容。
file = open("filename.txt", "r") 
readline = file.readline().rstrip()
while readline!= "":
    list = []
    list = readline.split(" ")
    j = list.index("x")
    k = list[0:j]
    v = list[j + 1:]
    d = {}
    if k not in d == False:
        d[k] = []
    d[k].append(v)
    readline = file.readline().rstrip()

我一直遇到一个TypeError: unhashable type: 'list'的错误。我知道字典的键不能是列表,但我想把值变成列表,而不是键。我想知道我是否在某个地方犯了错误。
7个回答

79

正如其他答案所指出的那样,错误在于 k = list[0:j],其中您的键被转换为列表。您可以尝试重新编写代码以利用split函数:

# Using with ensures that the file is properly closed when you're done
with open('filename.txt', 'rb') as f:
  d = {}
  # Here we use readlines() to split the file into a list where each element is a line
  for line in f.readlines():
    # Now we split the file on `x`, since the part before the x will be
    # the key and the part after the value
    line = line.split('x')
    # Take the line parts and strip out the spaces, assigning them to the variables
    # Once you get a bit more comfortable, this works as well:
    # key, value = [x.strip() for x in line] 
    key = line[0].strip()
    value = line[1].strip()
    # Now we check if the dictionary contains the key; if so, append the new value,
    # and if not, make a new list that contains the current value
    # (For future reference, this is a great place for a defaultdict :)
    if key in d:
      d[key].append(value)
    else:
      d[key] = [value]

print d
# {'AAA': ['111', '112'], 'AAC': ['123'], 'AAB': ['111']}
请注意,如果您正在使用Python 3.x,您需要进行一些小的调整才能使其正常工作。如果您使用rb打开文件,则需要使用line = line.split(b'x')(确保您正在将字节拆分为正确类型的字符串)。您还可以使用with open('filename.txt', 'rU') as f:(甚至使用with open('filename.txt', 'r') as f:)打开文件,它应该可以正常工作。

79
注意:此答案没有明确回答所提出的问题,其他答案已经给出了解答。由于问题是特定场景相关的,而引发的异常是普遍存在的,因此本答案指向一般情况。
哈希值只是用于在字典查找期间快速比较字典键的整数。
内部上,hash() 方法调用对象的 __hash__() 方法,这些方法默认设置为任何对象。

将嵌套列表转换为集合

>>> a = [1,2,3,4,[5,6,7],8,9]
>>> set(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

这是因为列表中包含一个无法哈希的列表。可以通过将内部嵌套列表转换为元组来解决这个问题。
>>> set([1, 2, 3, 4, (5, 6, 7), 8, 9])
set([1, 2, 3, 4, 8, 9, (5, 6, 7)])

明确地对嵌套列表进行哈希处理

>>> hash([1, 2, 3, [4, 5,], 6, 7])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'


>>> hash(tuple([1, 2, 3, [4, 5,], 6, 7]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

>>> hash(tuple([1, 2, 3, tuple([4, 5,]), 6, 7]))
-7943504827826258506

避免这个错误的解决方案是将列表重构为嵌套元组而不是列表。

5
如果列表太大怎么办?这看起来是一个不错的解决方案,但并不够通用。 - msh855
2
@msh855 有大小限制吗?我使用了包含10万个元素的元组测试了该字典,在我使用的Python 3.6版本中运行良好。 - Sreram
谢谢您解释原因。这让我明白了,在尝试用两种方法解决问题时,两者都有“列表”类型,但其中一个出现了错误。 - Vers
1
@msh855,当给定大小为0或者一千万亿的列表时,无法进行哈希。这是类型而不是大小的问题。列表没有__hash__方法。解决方法是创建一个继承自list并具有__hash__()方法的custom_list类型,然后将您的列表转换为使用custom_list数据类型。但最好还是使用内置类型。 - All Іѕ Vаиітy

28

您试图将列表 k 用作字典的键 d,但是列表是可变的,不能用作字典的键。

此外,由于此行代码: ,您从未初始化字典中的列表。

if k not in d == False:

应该是:

if k not in d == True:

实际上应该是:

if k not in d:

6
你之所以会收到“不可哈希类型:'list'”异常,是因为k = list[0:j]k设置为列表的“切片”,逻辑上是另一个更短的列表。你需要做的是只获取列表中的第一个项目,写成k = list[0]。同样,v = list[j + 1:]应该只是v = list[2],对于从调用readline.split(" ")返回的列表的第三个元素。

我注意到代码中还有其他可能存在的问题,其中我会提到一些。其中一个大问题是你不想为循环中读取的每行(重新)初始化d,即d = {}。另一个问题通常不是一个好主意,将变量命名为任何内置类型的名称,因为如果需要访问其中一个变量,它将防止你访问其中一个变量,并且对于习惯于这些标准项指定名称的其他人来说,这是令人困惑的。因此,你应该将list变量重命名为其他名称,以避免出现此类问题。

这是一个具有改动的可用版本,我还用字典的setdefault()方法替换了你用来检查键是否已在字典中的if语句表达式,使处理变得更加简洁。
d = {}
with open("nameerror.txt", "r") as file:
    line = file.readline().rstrip()
    while line:
        lst = line.split() # Split into sequence like ['AAA', 'x', '111'].
        k, _, v = lst[:3]  # Get first and third items.
        d.setdefault(k, []).append(v)
        line = file.readline().rstrip()

print('d: {}'.format(d))

输出:

d: {'AAA': ['111', '112'], 'AAC': ['123'], 'AAB': ['111']}

2
这是因为该列表包含值列表,例如:
a = [[1,2],[1,2],[3,4]]

这种方法无法处理类似这样的情况:

list(set(a))

为了解决这个问题,你可以将内部列表转换为元组,例如:
a = [(1,2),(1,2),(3,4)]

这将有效!

0

TypeError 的发生是因为 k 是一个列表,因为它是使用另一个列表的切片创建的,代码行为 k = list[0:j]。这应该改为类似于 k = ' '.join(list[0:j]),这样你就有了一个字符串。

除此之外,你的 if 语句是不正确的,正如 Jesse 的回答所指出的那样,应该写成 if k not in d 或者 if not k in d(我更喜欢后者)。

你还在每次迭代中清空字典,因为你在 for 循环内部有 d = {}

请注意,你也不应该使用 listfile 作为变量名,因为这会掩盖内置函数。

以下是我重写你的代码的方式:

d = {}
with open("filename.txt", "r") as input_file:
    for line in input_file:
        fields = line.split()
        j = fields.index("x")
        k = " ".join(fields[:j])
        d.setdefault(k, []).append(" ".join(fields[j+1:]))

上面的dict.setdefault()方法替换了你代码中的if k not in d逻辑。


虽然偏好是您的完全权利,但“not k in d”可能会让初学者感到困惑,因为它可以被理解为“(not k) in d”,而“k not in d”则没有歧义。 - Jesse the Game
我甚至会认为这是“Pythonic”的方式,因为not in被列为运算符 - Jesse the Game
是的,我认为我的偏好可能来自于先学习其他语言,在这些语言中,对于像包含测试这样的操作,你不会有专门的运算符,所以你会做类似于 !a.contains(b) 的事情。not in 可能更符合 Python 风格,但我觉得两个单词组成的运算符的概念比在布尔表达式上使用反向操作更令人困惑。 - Andrew Clark

-1
    python 3.2

    with open("d://test.txt") as f:
              k=(((i.split("\n"))[0].rstrip()).split() for i in f.readlines())
              d={}
              for i,_,v in k:
                      d.setdefault(i,[]).append(v)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接