Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的功能有什么问题?使用numpy和networkx查找最短路径_Python_Function_Numpy_For Loop_Networkx - Fatal编程技术网

Python 我的功能有什么问题?使用numpy和networkx查找最短路径

Python 我的功能有什么问题?使用numpy和networkx查找最短路径,python,function,numpy,for-loop,networkx,Python,Function,Numpy,For Loop,Networkx,因此,我正在创建一个代码来使用networkx计算最短路径。我使用numpy创建了一个3D阵列,然后计算了如下最短路径: import numpy as np import networkx as nx arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array for i in arr: graph = nx.from_numpy_array(i, create_using = nx.DiGraph) path =

因此,我正在创建一个代码来使用networkx计算最短路径。我使用numpy创建了一个3D阵列,然后计算了如下最短路径:

import numpy as np
import networkx as nx

arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array

for i in arr:
    graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
    path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    print(path)

import numpy as np
import networkx as nx

arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array


def shortest(prices):

    for i in arr:
        graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
        path = nx.shortest_path(graph, 0, 3, weight = 'weight')
        return path

print(shortest(arr))
def shortest(precios):

    for i in arr:
        graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
        path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    return path

print(shortest(arr))
因为我使用了两个矩阵,所以我得到了下一个输出:

[0, 1, 3] #path1
[0, 3]    #path2
[0, 1, 3] #same as path 1
[0, 3] #same as path 2
之后,我决定创建一个函数来执行完全相同的操作,如下所示:

import numpy as np
import networkx as nx

arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array

for i in arr:
    graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
    path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    print(path)

import numpy as np
import networkx as nx

arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array


def shortest(prices):

    for i in arr:
        graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
        path = nx.shortest_path(graph, 0, 3, weight = 'weight')
        return path

print(shortest(arr))
def shortest(precios):

    for i in arr:
        graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
        path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    return path

print(shortest(arr))
我得到了下一个输出:

[0, 1, 3] #path1
[0, 3]    #path2
[0, 1, 3] #same as path 1
[0, 3] #same as path 2
如果我像这样改变“返回路径”的位置:

import numpy as np
import networkx as nx

arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array

for i in arr:
    graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
    path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    print(path)

import numpy as np
import networkx as nx

arr = np.random.randint(1, 100, size = (2, 5, 5)) #3D array


def shortest(prices):

    for i in arr:
        graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
        path = nx.shortest_path(graph, 0, 3, weight = 'weight')
        return path

print(shortest(arr))
def shortest(precios):

    for i in arr:
        graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
        path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    return path

print(shortest(arr))
我得到了下一个输出:

[0, 1, 3] #path1
[0, 3]    #path2
[0, 1, 3] #same as path 1
[0, 3] #same as path 2

我无法使用我的函数在同一输出中获得两条路径的值。知道这里发生了什么吗?我一直在练习用python函数,因为我对这个主题有点陌生,所以我希望你能帮我看看哪里出了问题?谢谢大家!

这与
networkx
numpy
无关;这是一个简单的控制流。您的“最短路径”循环遍历提供的图形,并为每个图形找到最短路径

非函数版本会按找到的路径打印每个路径,但每次只处理一个路径。由于您在函数中没有规定聚合找到的解决方案,因此一次只能得到一个解决方案。。。对于一个函数,这意味着你只能得到一个

此代码在找到第一个解决方案后立即返回:

for i in arr:
    graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
    path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    return path   # The function ends as soon as it gets here: you get only the first solution.
此代码可以找到所有解决方案,但一旦找到下一个解决方案,您就会将每个解决方案都扔掉:

for i in arr:
    graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
    path = nx.shortest_path(graph, 0, 3, weight = 'weight')
    # You just deleted the previous solution, and replaced it with another.

return path
这只返回最后一个解决方案,因为这是唯一一个您没有覆盖的解决方案。您必须以某种方式累积解决方案并将它们一起返回,或者改变您的函数用法。例如:

all_path = []
for i in arr:
    graph = nx.from_numpy_array(i, create_using = nx.DiGraph)
    all_path.append( nx.shortest_path(graph, 0, 3, weight = 'weight') )

return all_path

哦!我明白了,所以我创建了一个新的列表并存储了解决方案!非常感谢你!