我正试图拿一个看起来像这样的文件
AAA x 111
AAB x 111
AAA x 112
AAC x 123
...
Run Code Online (Sandbox Code Playgroud)
并使用字典,以便输出看起来像这样
{AAA: ['111', '112'], AAB: ['111'], AAC: [123], ...}
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的
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()
Run Code Online (Sandbox Code Playgroud)
我一直在接受TypeError: unhashable type: 'list'.我知道字典中的键不能是列表,但我试图将我的值变成列表而不是键.我想知道我是否在某处犯了错误.