python - 如何通过用户输入在 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
结果不是我想要的,因为我想要每个顶点都带有成本的多重边
解决方案
推荐阅读
- javascript - 如何包装一个段落
- sql-server - SQL Server 中的唯一计数
- java - Spring Boot 未在 Tomcat 实例中启动
- javascript - 使用拖放将项目放置在列表之间
- javascript - 类型错误:repos.map 不是函数
- xamarin - 使用 Telerik 库后缺少带有目的字符串的 NSCalendarsUsageDescription 键
- elixir - 长生不老药 lua 日历在 shitf_zone 时显示不同的行为
- java - 方法不存在类链接问题:在两个链接的 Java 项目中从具有相同名称和项目路径的类调用方法时
- linux - dnssec-dsfromkey 显示未知算法错误
- json - 使用 Node.js Express 检查 POST 正文请求语法和格式