per node retrieved by dictionary using nx.short_path_length
I would like to extract the distance from the starting point as a list, but the code below is
There is an error.What should I do?
d=nx.shortest_path_length(G, source="General Customer", target=None)
f = list(d.values())
node_color=[f[node] for node in G.nodes()]
node_color
TypeError Traceback (most recent call last)
<ipython-input-44-3dc896932224>in<module>
1 d = nx.shortest_path_length (G, source = "General Customer", target = None)
2f = list(d.values())
---->3node_color=[f[node] for node in G.nodes()]
4node_color
<ipython-input-44-3dc896932224>in<listcomp>(.0)
1 d = nx.shortest_path_length (G, source = "General Customer", target = None)
2f = list(d.values())
---->3node_color=[f[node] for node in G.nodes()]
4node_color
TypeError: list indications must be integrators or slices, not str
First of all,
G.nodes()
Check the contents of the .Is the element a string?
This is why f[node] has failed.
In some cases, use d in the dictionary as it is
node_color=[d[node] for node in G.nodes()]
But you may get the desired value.
© 2025 OneMinuteCode. All rights reserved.