【问题标题】:Value return problem in function of python classpython类函数中的值返回问题
【发布时间】:2019-06-27 02:06:03
【问题描述】:

所以我正在做一个项目,我必须找到最好的路径......所以我现在为此使用了 DFS,问题是在递归调用时它会打印所有可能的结果,最后最好的。但是这些值并没有存储在类的“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 如果您要复制,那是错误的做法。试试看:self.PathList = self.stack[:]
  • @BanishedBot ...非常感谢它成功了
  • 太棒了!我将其发布为答案。

标签: python python-3.x class recursion depth-first-search


【解决方案1】:

这是因为您实际上并没有通过这样做来复制stack

self.PathList = self.stack

替换为

self.PathList = self.stack[:]

self.PathList = list(self.stack)

【讨论】:

    【解决方案2】:

    问题在于您没有创建最佳路径的副本。你应该这样做:

    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)
    

    我没有测试代码,因为我没有测试数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-08
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多