如何克服 TypeError:不可哈希类型:'list' [重复]
- 2025-03-04 08:27:00
- admin 原创
- 89
问题描述:
我正在尝试获取如下所示的文件:
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'
。我知道字典中的键不能是列表,但我试图将值放入列表而不是键中。我想知道我是否在某个地方犯了错误。
解决方案 1:
注意:
此答案没有明确回答所提出的问题。其他答案都回答了。由于问题特定于场景,并且引发的异常是一般情况,因此此答案指向一般情况。
哈希值只是整数,用于在字典查找过程中快速比较字典键。
在内部,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
避免此错误的解决方案是重构列表以使其具有嵌套元组而不是列表。
解决方案 2:
正如其他答案所指出的,错误是由于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:`
解决方案 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:]
应该只是v = list[2]
调用 返回的列表的第三个元素readline.split(" ")
。
我注意到代码中可能还存在其他几个问题,我将列举其中几个。一个大问题是您不想在循环中读取每一行时(重新)初始化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 d
或if not k in d
(我更喜欢后者)。
d = {}
由于您的字典位于循环内部,因此您还会在每次迭代中清除字典for
。
请注意,您也不应使用list
或file
作为变量名,因为您将屏蔽内置函数。
以下是我重写代码的方法:
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
中的逻辑。
解决方案 7:
蟒蛇3.2
with open("d://test.txt") as f:
k=(((i.split("
"))[0].rstrip()).split() for i in f.readlines())
d={}
for i,_,v in k:
d.setdefault(i,[]).append(v)
扫码咨询,免费领取项目管理大礼包!