如何解决 TypeError: unhashable type: 'list' 错误。
如何解决 TypeError: unhashable type: 'list' 错误。
这个问题已经有了答案:
我正在尝试处理一个看起来像这样的文件:
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\'
。我知道字典中的键不能是列表,但我正在尝试将值变成一个列表,而不是将键变成一个列表。我想知道是否我在哪里犯了一个错误。
admin 更改状态以发布 2023年5月22日
注意:这个答案并没有明确回答问题。其他答案已经回答了。因为问题特定于一种情况而抛出的异常是一般性的,所以这个答案指向了一般情况。
哈希值只是用于在字典查找时快速比较字典键的整数。
在内部,hash()
方法调用对象的__hash__()
方法,默认为任何对象设置。
将嵌套列表转换为集合
>>> a = [1,2,3,4,[5,6,7],8,9] >>> set(a) Traceback (most recent call last): File "", line 1, in 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 "", line 1, in TypeError: unhashable type: 'list' >>> hash(tuple([1, 2, 3, [4, 5,], 6, 7])) Traceback (most recent call last): File " ", line 1, in TypeError: unhashable type: 'list' >>> hash(tuple([1, 2, 3, tuple([4, 5,]), 6, 7])) -7943504827826258506
避免这个错误的解决方案是将列表重构为嵌套元组而不是列表。
正如其他答案所表明的,错误是由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:
)打开文件,它应该能正常工作。