首页 > 解决方案 > 如何通过用户输入在 Python 中创建字典

问题描述

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

我想要的python格式是嵌套的dict到dict:

   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

结果不是我想要的,因为我想要每个顶点都带有成本的多重边

标签: pythondictionarypython-3.7

解决方案


推荐阅读