![](/img/trans.png)
[英]What's the difference between these codes? Why do they have different outputs?
[英]what's the difference between these 2 python codes? why different results?
我正在编写如下的dfs代码:
def dfs(self, graph, node, path):
if node==len(graph)-1:
self.res.append(path)
else:
for i in graph[node]:
path.append(i)
self.dfs(graph, i, path)
print(path.pop())
但是得到了不希望的结果,而我将dfs中的for循环代码更改为:
self.dfs(graph, i, path+[i])
结果就是我想要的。 但是我不知道这两个代码有什么区别。 谢谢
在算法方面,我需要有关如何表示图形的更多信息,以提供更多见解。 但是在代码方面,差异是由于Python中的List是可变的这一事实引起的。 这意味着在您的原始代码中,当您pop
path
列表时, res
的条目将相应更改。 通过以下检查。
a = [1,2,3]
b = []
b.append(a) # b = [[1,2,3]]
a.pop() # now b = [[1,2]]
但是,当您将参数更改为path + [i]
,将构造一个新的List,即,下一个dfs
递归调用中的path
与上一个中的path
解耦。 通过以下检查。
a = [1,2]
b = []
b.append(a+[3]) # b = [[1,2,3]]
a.pop()
# b is still [[1,2,3]], because the expression a + [3] will be evaluated to another List
原始版本会在每次递归的每次迭代中更改路径。 第二个版本只是复制并添加了一个额外的元素。 此副本未绑定到名称,它只是传递到下一级递归。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.