【发布时间】:2018-04-20 11:19:16
【问题描述】:
我有 python networkX 代码的输出:
flow_value, flow_dict = nx.maximum_flow(T, 'O', 'T')
print(flow_dict)
#Output as followesenter
#{'O': {'A': 4, 'B': 6, 'C': 4}, 'A': {'B': 1, 'D': 3}, 'B': {'C': 0, 'E': 3,'D': 4}, 'C': {'E': 4}, 'E': {'D': 1, 'T': 6}, 'D': {'T': 8}, 'T': {}}
我要提取的表格中的所有数据如下:
#('O','A',4),('O','B','6'),('O','C','4'),('A','B',1),......,('D','T',8)
我有什么方法可以遍历嵌套的 dict 并获取我需要的数据?
【问题讨论】:
-
是的,你试过什么?您是否查看过“如何在 Python 中迭代字典”?
-
[(key, subkey, value) for key, subdict in flow_dict.items() for subkey, value in subdict.items()] -
彼得成功了!!!
标签: python python-3.x dictionary nested networkx