
进行一些试验,
nx.dijkstra_path当源节点和目标节点相同时,似乎会引发误导性异常:
>>> import networkx as nx>>> g = nx.Graph()>>> g.add_edge('a', 'b', distance=0.3)>>> g.add_edge('a', 'c', distance=0.7)>>> nx.dijkstra_path_length(g, 'b', 'c', 'distance')1.0>>> nx.dijkstra_path(g, 'b', 'c', 'distance')['b', 'a', 'c']>>> nx.dijkstra_path_length(g, 'b', 'b', 'distance')0>>> nx.dijkstra_path(g, 'b', 'b', 'distance')Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> nx.dijkstra_path(g, 'b', 'b', 'distance') File "C:UsersbarbermAppDataRoamingPythonPython27site-packagesnetworkxalgorithmsshortest_pathsweighted.py", line 74, in dijkstra_path return path[target]TypeError: list indices must be integers, not str因此,只需进行一次显式测试,确定
destination和
origin是否相同,然后分别进行处理即可。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)