【发布时间】:2017-04-18 03:56:14
【问题描述】:
我正在尝试找出两者之间的区别
- 路径 = 路径 + [node1]
- 路径 += [node1]
- path.append(node1)
我得到了 path = path + [node1] 的正确路径,但不是其他两个。
def find_path(self, node1, node2, path = []):
""" Finds any path from node1 to node2 which may not be the shortest """
path = path + [node1]
if node1 == node2:
return path
if node1 not in self._graph:
return None
for node in self._graph[node1]:
if node not in path:
new_path = self.find_path(node, node2, path)
if new_path:
return new_path
return None
【问题讨论】:
-
什么是节点?您是否收到错误消息或不正确的结果?
-
它们只是来自 [('A', 'B'), ('B', 'C'), ('B', 'D' ), ('C', 'D'), ('E', 'F'), ('F', 'C')].
标签: python dictionary graph path set