想要获得某两点间的所有路径,将这些路径都放到一个数组中。获得路径的代码没有问题,但是不知道怎么把他们都放到一个数组path[]中,每次path返回的都是空值
stack = []
paths = []
path = []
def path_between_nodes(start, end):
stack.append(start)
if start == end:
# print("找到路径:", stack)
path.append(stack)
stack.pop()
# return path
else:
for nextPoint in neighbor[start]:
if nextPoint not in stack and nextPoint != 0: path_between_nodes(nextPoint, end)
stack.pop()
return path
输出的path是 [[],[],……]
希望能将两点间的所有路径放到一个数组中
深浅拷贝的问题,因为stack发生了变化(最后都变成空列表),所以path也变化了
把这一句
path.append(stack)
改成
path.append(stack.copy())
再试试