【发布时间】:2020-07-29 10:16:19
【问题描述】:
我有一个文件,其中每行有 2 个名称。 假设我有以下输入:
名字1 名字2
名称 3 名称 4
名字 1 名字 5
我想做一个这样的字典:
name1 : [name2, name5]
名称2:名称1
名称 3 : 名称 4
名称 4 : 名称 3
name5 : name1
这是我制作的代码,但我不知道我做错了什么..
d = {}
for i in range(len(l)): # consider l the input
d[l[i]] = ""
for i in range(0, len(l), 2):
e1 = l[i]
e2 = l[i+1]
d.update({e1 : [d[e1], e2]}) #i think the update operation is wrong here..
d.update({e2 : [d[e2], e1]})
【问题讨论】:
-
您能否添加程序的输出(例如:print(d)),以便更容易理解出了什么问题?
-
...开头也是
print(l)。
标签: python dictionary