我正试图拿一个看起来像这样的文件
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'.我知道字典中的键不能是列表,但我试图将我的值变成列表而不是键.我想知道我是否在某处犯了错误.
我有两个列表如下
x = ['a','a','b','c','b','a']
Run Code Online (Sandbox Code Playgroud)
和
x = ['a','a','b','c','c','d']
Run Code Online (Sandbox Code Playgroud)
我试图找出每个列表中出现最多的值.这就是我尝试过的.
def unique_values(output,input):
for i in input:
if i not in output:
output.append(i)
k = []
for i in k:
unique_values(k,x)
y.remove(i)
Run Code Online (Sandbox Code Playgroud)
我已经做到这一点,但我无法弄清楚如何for i in k:在它删除列表中的所有值之前停止.