【问题标题】:How to overcome TypeError: unhashable type: 'list'如何克服 TypeError: unhashable type: 'list'
【发布时间】:2025-12-20 08:50:06
【问题描述】:

我正在尝试获取如下所示的文件:

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'。我知道字典中的键不能是列表,但我试图将我的值变成列表而不是键。我想知道我是否在某个地方犯了错误。

【问题讨论】:

    标签: python list dictionary typeerror


    【解决方案1】:

    正如其他答案所示,错误是由于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:)打开文件,它应该可以正常工作。

    【讨论】:

      【解决方案2】:

      注意: 这个答案没有明确回答所提出的问题。其他答案做到了。由于问题是特定于场景并且提出的异常是一般情况,因此此答案指向一般情况。

      哈希值只是用于在字典查找过程中快速比较字典键的整数。

      在内部,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
      

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

      【讨论】:

      • 如果列表太大怎么办??看起来这是一个很好的解决方案,但不够通用
      • @msh855 有大小限制吗?我用大小为 100,000 的元组测试了字典,对我来说效果很好(我使用的是 python 3.6)
      • 感谢您解释原因。这消除了我在尝试使用 2 种方法解决问题时的疑问,并且两种方法的类型都为“列表”,但其中一个给出了此错误。
      • @msh855 你不能散列一个给定大小的列表0quadrillion。这是类型而不是大小的问题。 列表没有__hash__ 方法。一种解决方法是创建一个custom_list 类型,该类型使用__hash__() 方法继承list,然后将您的列表转换为使用custom_list 数据类型。还是使用内置类型更好。
      【解决方案3】:

      您正在尝试使用k(这是一个列表)作为d 的键。列表是可变的,不能用作字典键。

      此外,由于这一行,您永远不会初始化字典中的列表:

      if k not in d == False:
      

      应该是:

      if k not in d == True:
      

      实际上应该是:

      if k not in d:
      

      【讨论】:

        【解决方案4】:

        您获得unhashable type: 'list' 异常的原因是因为k = list[0:j]k 设置为列表的“切片”,从逻辑上讲,它是另一个列表,通常更短。您需要的是获取列表中的第一项,如k = list[0] 所示。 v = list[j + 1:] 也一样,对于从对 readline.split(" ") 的调用返回的列表的第三个元素,它应该只是 v = list[2]

        我注意到代码还有其他几个可能的问题,我将在其中提到一些。一个重要的问题是您不想为循环中读取的每一行使用d = {}(重新)初始化d。另一个是,将变量命名为与任何内置类型相同的名称通常不是一个好主意,因为它会阻止您在需要时访问它们中的一个——这会让习惯于指定这些标准项目之一的名称。因此,您应该将变量 list 变量重命名为不同的名称,以避免出现此类问题。

        这是您的工作版本,其中包含这些更改,我还替换了您用于检查键是否已在字典中的 if 语句表达式,现在使用字典的 setdefault() 方法来更简洁地完成同样的事情。

        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']}
        

        【讨论】:

          【解决方案5】:

          这背后的原因是列表包含值列表。喜欢:

          a = [[1,2],[1,2],[3,4]]
          

          这不适用于这样的事情:

          list(set(a))
          

          要解决这个问题,您可以将内部列表转换为元组,例如:

          a = [(1,2),(1,2),(3,4)]
          

          这行得通!

          【讨论】:

            【解决方案6】:

            TypeError 正在发生,因为 k 是一个列表,因为它是使用来自另一个列表的切片创建的,其中包含行 k = list[0:j]。这应该类似于k = ' '.join(list[0:j]),所以你有一个字符串。

            除此之外,您的 if 声明不正确,正如 Jesse 的回答所指出的那样,应该是 if k not in dif 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 没有歧义
            • 我什至认为这是“pythonic”方式,因为not in 被列为operator
            • 是的,我认为我的偏好可能来自于首先学习其他语言,对于像遏制测试这样的东西,你不会有操作员,所以你会做像!a.contains(b) 这样的事情。 not in 可能更 Pythonic,我只是发现两个单词运算符的概念比在布尔表达式上使用逆运算符更令人困惑。
            【解决方案7】:
                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)
            

            【讨论】:

              最近更新 更多