小编Biy*_*ang的帖子

python - 如何在循环中创建字典字典

我正在尝试从文件生成城市图。每个readline()我都得到三个值ab、 和ca并且b是字符串,城市名称,cint,之间的路径成本ab。我如何存储a, b,c以便我得到类似的东西

graph = {
          'a1': {'b1': c1, 'b1': 5, 'a2': c2},
          'a2': {'a1': c2},
          'b1': {'a1': c1},
        }
Run Code Online (Sandbox Code Playgroud)

这是我的代码的一部分:

dict = dict()   #dictionary for storing values
while (True):
    new_path = file.readline()
    if new_path == "":
        break
    new_path = new_path.rstrip().split(", ")
    a = new_path[0][2:len(new_path[0]) - 1] #start town
    b = new_path[1][1:len(new_path[1]) - 1] #end …
Run Code Online (Sandbox Code Playgroud)

dictionary graph python-3.x

5
推荐指数
1
解决办法
2200
查看次数

标签 统计

dictionary ×1

graph ×1

python-3.x ×1