【发布时间】:2021-10-20 17:18:45
【问题描述】:
我有以下最短路径
{'A': {'B': ['A', 'B'],
'A': ['A'],
'C': ['A', 'C']},
'D': {'A': ['D', 'A'],
'B': ['D', 'B'],
'C': ['D', 'C']}}
如下构建:
selected_shortest = {source: {target: nx.shortest_path(G, source, target) for target in list4path if nx.has_path(G, source, target)} for source in G.nodes()}
其中 G 是一个网络
G = nx.from_pandas_edgelist(edges, source='Node', target='Target')
而list4path 是潜在目标的列表(例如,A、B 和 C)。
我想获得它们之间的距离(或间隔),而不是列表中源和目标之间的节点名称。所以,例如,
{'A': {'B': 1,
'A': 0
'C': 1},
'D': {'A': 1,
'B': 1,
'C': 1}}
为了取这些计算出的最短路径的平均值。 你能帮我得到这些平均值吗?
【问题讨论】:
-
似乎你可以将所有列表值设置为 len(list)-1,不是吗?
标签: python networkx shortest-path