我想在输入值中设置python中的多个字典值,但总是返回错误通知。但如果我在脚本中声明字典,它将运行良好。所以,我想:从输入值中设置多个字典值?
adj=defaultdict(list)
iteration=input("the number of edges that constructed: ")
for i in range (0,int(iteration)):
#A(vertices1) B(vertices2) W(weight)
abw=input("A B W : ")
if len(abw)==1:
a=int(abw)
valueBW=(None)
if a in adj:
adj[a].append(())
else:
adj[a].append(())
#dict.fromkeys(a,None)
else:
a,b,w=abw.split(' ')
a=int(a)
valueBW=(int(b),int(w))
if a in adj:
adj[a].append(valueBW)
else:
#adj.update({a : [(int(b),int(w))]})
adj[a].append(valueBW)
这是输入例如:
the number of edges that constructed: 8
A B W : 0 1 4
A B W : 0 3 8
A B W : 1 4 1
A B W : 1 2 2
A B W : 4 2 3
A B W : 2 5 3
A B W : 3 4 2
A B W : 5
这是字典,如果我在代码中声明它:
adj = {
0: [(1, 4),(3, 8)],
1: [(4, 1),(2, 2)],
4: [(2, 3)],
2: [(5, 3)],
3: [(4, 2)],
5: [],
}
是我的代码吗?
你有什么错误? –
如果出现错误,请发布* entire * traceback。 –
好的谢谢。幸运的是,我的问题已得到解决 –