return python class 函数中的值 return 问题
Value return problem in function of python class
所以我在做一个项目,因为我必须找到最好的可能路径...所以我现在使用 DFS,问题是在递归调用时它会打印所有可能的结果并且在最后最好的。但是这些值没有存储在 class 的 "PathList" 变量列表中。
例如:Graph.py 是存储图形的另一个文件
import Graph
class G:
def __init__(self,Graph,vis):
self.graph = Graph
self.dist=1000000
self.PathList= []
self.stack=[]
self.visited=vis
def addEdge(self, u,v):
self.graph[u].append(v)
def DFScall(self,v,f,dis):
self.visited[v]=True
self.stack.append(v)
dis+=1
if(v==f):
if(dis<self.dist):
self.PathList = self.stack
self.dist=dis
#print(self.PathList)
else :
for i in self.graph[v]:
if self.visited[i] == False:
#print(self.visited[i])
self.DFScall(i,f,dis)
self.stack.pop()
self.visited[v]=False
def DFS(self,v,f):
self.DFScall(v,f,0)
def returnPath(self):
return self.PathList
if __name__== "__main__":
graph=Graph.returnGraph()
vis=Graph.returnNodes()
g = G(graph,vis)
g.DFS('S1','B8')
Pathlist = g.returnPath()
print(Pathlist)
输出为:
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'G2', 'H3', 'I2', 'J3', 'J4', 'I5', 'H4', 'G5', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'G2', 'H3', 'H4', 'G5', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'C6', 'D7', 'D8', 'C9', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'E2', 'D3', 'D4', 'C5', 'C6', 'B7', 'B8']
[]
最后,list输出是通过main调用时的输出.....这种情况我应该怎么办,告诉我有没有什么需要研究的。
谢谢
问题是您没有创建最佳路径的副本。你应该这样做:
self.PathList = self.stack.copy()
一个完整的例子(为提高可读性修改了 PEP8):
import Graph
class GraphSearch:
def __init__(self, graph, nodes):
self.graph = graph.copy()
self.nodes = nodes.copy()
self.visited = None
self.best_path_list = None
self.current_path_list = None
def reset_search(self):
self.visited = {k: False for k, v in self.nodes.items()}
self.best_path_list = list()
self.current_path_list = list()
def _dfs_call(self, current_node, end_node):
self.visited[current_node] = True
self.current_path_list.append(current_node)
if(current_node == end_node):
current_distance = len(self.current_path_list)
best_distance = len(self.best_path_list)
if(current_distance < best_distance):
self.best_path_list = self.current_path_list.copy()
else:
for adj_node in self.graph[current_node]:
if not self.visited[adj_node]:
self._dfs_call(adj_node, end_node)
self.current_path_list.pop()
self.visited[current_node] = False
def dfs(self, current_node, end_node):
self.reset_search()
self._dfs_call(current_node, end_node)
if __name__ == "__main__":
graph = Graph.return_graph()
nodes = Graph.return_nodes()
graph_search = GraphSearch(graph, nodes)
start_node = 'S1'
end_node = 'B8'
graph_search.dfs(start_node, end_node)
print(graph_search.best_path_list)
我没有测试代码,因为我没有测试数据。
这是因为您实际上并没有通过
复制stack
self.PathList = self.stack
替换为
self.PathList = self.stack[:]
或
self.PathList = list(self.stack)
所以我在做一个项目,因为我必须找到最好的可能路径...所以我现在使用 DFS,问题是在递归调用时它会打印所有可能的结果并且在最后最好的。但是这些值没有存储在 class 的 "PathList" 变量列表中。
例如:Graph.py 是存储图形的另一个文件
import Graph
class G:
def __init__(self,Graph,vis):
self.graph = Graph
self.dist=1000000
self.PathList= []
self.stack=[]
self.visited=vis
def addEdge(self, u,v):
self.graph[u].append(v)
def DFScall(self,v,f,dis):
self.visited[v]=True
self.stack.append(v)
dis+=1
if(v==f):
if(dis<self.dist):
self.PathList = self.stack
self.dist=dis
#print(self.PathList)
else :
for i in self.graph[v]:
if self.visited[i] == False:
#print(self.visited[i])
self.DFScall(i,f,dis)
self.stack.pop()
self.visited[v]=False
def DFS(self,v,f):
self.DFScall(v,f,0)
def returnPath(self):
return self.PathList
if __name__== "__main__":
graph=Graph.returnGraph()
vis=Graph.returnNodes()
g = G(graph,vis)
g.DFS('S1','B8')
Pathlist = g.returnPath()
print(Pathlist)
输出为:
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'G2', 'H3', 'I2', 'J3', 'J4', 'I5', 'H4', 'G5', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'G2', 'H3', 'H4', 'G5', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'C6', 'D7', 'D8', 'C9', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'E2', 'D3', 'D4', 'C5', 'C6', 'B7', 'B8']
[]
最后,list输出是通过main调用时的输出.....这种情况我应该怎么办,告诉我有没有什么需要研究的。
谢谢
问题是您没有创建最佳路径的副本。你应该这样做:
self.PathList = self.stack.copy()
一个完整的例子(为提高可读性修改了 PEP8):
import Graph
class GraphSearch:
def __init__(self, graph, nodes):
self.graph = graph.copy()
self.nodes = nodes.copy()
self.visited = None
self.best_path_list = None
self.current_path_list = None
def reset_search(self):
self.visited = {k: False for k, v in self.nodes.items()}
self.best_path_list = list()
self.current_path_list = list()
def _dfs_call(self, current_node, end_node):
self.visited[current_node] = True
self.current_path_list.append(current_node)
if(current_node == end_node):
current_distance = len(self.current_path_list)
best_distance = len(self.best_path_list)
if(current_distance < best_distance):
self.best_path_list = self.current_path_list.copy()
else:
for adj_node in self.graph[current_node]:
if not self.visited[adj_node]:
self._dfs_call(adj_node, end_node)
self.current_path_list.pop()
self.visited[current_node] = False
def dfs(self, current_node, end_node):
self.reset_search()
self._dfs_call(current_node, end_node)
if __name__ == "__main__":
graph = Graph.return_graph()
nodes = Graph.return_nodes()
graph_search = GraphSearch(graph, nodes)
start_node = 'S1'
end_node = 'B8'
graph_search.dfs(start_node, end_node)
print(graph_search.best_path_list)
我没有测试代码,因为我没有测试数据。
这是因为您实际上并没有通过
复制stack
self.PathList = self.stack
替换为
self.PathList = self.stack[:]
或
self.PathList = list(self.stack)