如何通过用户输入在Python中创建字典

我有一种具有网络顶点和边缘成本的特定格式的字典,我想通过用户输入来创建它

我想要的python格式是要嵌套的字典:

   graph = {'a':{'b':10,'c':3},'b':{'c':1,'d':2},'c'{'b':4,'d':8,'e':2},'d':{'e':7},'e':{'d':9}}

到目前为止的代码:

while True:        
graph = dict()

user_input = input("Enter key and value separated by commas (,): ")

key,value = user_input.split(",")
if user_input == "":
  break

my_dict[key] = value

结果不是我想要的,因为我要为每个顶点加上成本的多重边缘

fanabcdefg 回答:如何通过用户输入在Python中创建字典

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3121709.html

大家都在问