144

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

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

4

7 回答 7

72

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

于 2012-12-03T00:01:40.850 回答
65

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

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

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

发生这种情况是因为列表中的列表是无法散列的列表。这可以通过将内部嵌套列表转换为 tuple来解决,

>>> 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

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

于 2018-07-16T10:32:40.187 回答
27

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

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

if k not in d == False:

应该是:

if k not in d == True:

实际上应该是:

if k not in d:
于 2012-12-02T23:52:14.453 回答
6

您收到unhashable type: 'list'异常的原因是因为k = list[0:j]设置k为列表的“切片”,从逻辑上讲,它是另一个列表,通常更短。你需要的是得到列表中的第一项,像这样写k = list[0]。从调用返回的列表的第三个元素v = list[j + 1:]应该是相同的。v = list[2]readline.split(" ")

我注意到代码还有其他几个可能的问题,我将在其中提到一些。一个重要的问题是您不想(重新)初始化循环dd = {}读取的每一行。另一个是,将变量命名为与任何内置类型相同的名称通常不是一个好主意,因为它会阻止您在需要时访问它们中的一个——而且这会让习惯于指定这些标准项目之一的名称。出于这个原因,您应该将变量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']}
于 2012-12-03T02:22:20.700 回答
2

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

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

这不适用于这样的事情:

list(set(a))

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

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

这会奏效!

于 2021-09-04T21:16:22.760 回答
0

TypeError发生这种情况是k因为它是一个列表,因为它是使用来自另一个列表的切片创建的,带有 line k = list[0:j]。这可能应该是类似的k = ' '.join(list[0:j]),所以你有一个字符串。

除此之外,if正如杰西的回答所指出的那样,您的陈述是不正确的,应该阅读if k not in dif not k in d(我更喜欢后者)。

d = {}由于您在循环内部,因此您还在每次迭代中清除字典for

请注意,您也不应该使用listorfile作为变量名,因为您将屏蔽内置函数。

以下是我将如何重写您的代码:

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代码中的逻辑。

于 2012-12-02T23:55:46.887 回答
-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)
于 2012-12-03T14:59:51.117 回答