Python:TypeError:unhashable类型:'list' [英] Python: TypeError: unhashable type: 'list'

查看:1277
本文介绍了Python:TypeError:unhashable类型:'list'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个看起来像这样的文件

I'm trying to take a file that looks like this

AAA x 111
AAB x 111
AAA x 112
AAC x 123
...

使用字典,以使输出看起来像这样

And use a dictionary to so that the output looks like this

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

ve尝试

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

I keep getting a TypeError: unhashable type: 'list'. I know that Keys in a dictionary can't be lists but I'm trying to make my value into a list not the key. I'm wondering if I made a mistake somewhere.

感谢所有帮助我的最后一个问题的人。

Thank you to everyone that helped me with my last question.

推荐答案

如其他答案所示,错误是由于 k = list [0:j] 键被转换为列表。您可以尝试的一件事是重新编码以利用 split 函数:

As indicated by the other answers, the error is to due to k = list[0:j], where your key is converted to a list. One thing you could try is reworking your code to take advantage of the split function:

# 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')(这确保你用适当类型的字符串分割字节)。您也可以使用open('filename.txt','rU')作为f:(甚至 with open('filename .txt','r')作为f:),它应该可以正常运行。

Note that if you are using Python 3.x, you'll have to make a minor adjustment to get it work properly. If you open the file with rb, you'll need to use line = line.split(b'x') (which makes sure you are splitting the byte with the proper type of string). You can also open the file using with open('filename.txt', 'rU') as f: (or even with open('filename.txt', 'r') as f:) and it should work fine.

这篇关于Python:TypeError:unhashable类型:'list'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆