Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表附加到列表时,为什么需要放置list()?_Python_Python 3.x - Fatal编程技术网

Python 将列表附加到列表时,为什么需要放置list()?

Python 将列表附加到列表时,为什么需要放置list()?,python,python-3.x,Python,Python 3.x,这就是我现在在Python3中正在解决的编码问题: class TreeNode: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def find_paths(root, targetSum): allPaths = [] dfs(root, targetSum, [], allPaths) return

这就是我现在在Python3中正在解决的编码问题:

class TreeNode:
  def __init__(self, val, left=None, right=None):
    self.val = val
    self.left = left
    self.right = right

def find_paths(root, targetSum):
  allPaths = []
  dfs(root, targetSum, [], allPaths)
  return allPaths
  
def dfs(node, targetSum, currentPath, result):
  
  if not node:
    return
  
  currentPath.append(node.val)

  if node.val == targetSum:
    if not node.left and not node.right:
      result.append(list(currentPath))
  else:
    dfs(node.left, targetSum - node.val, currentPath, result)
    dfs(node.right, targetSum - node.val, currentPath, result)

  # backtrack here
  del currentPath[-1]


def main():

  root = TreeNode(12)
  root.left = TreeNode(7)
  root.right = TreeNode(1)
  root.left.left = TreeNode(4)
  root.right.left = TreeNode(10)
  root.right.right = TreeNode(5)
  required_sum = 23
  print(find_paths(root, required_sum))


main()
问题就在这一行:

      result.append(list(currentPath))
它打印出:

[[12, 7, 4], [12, 1, 10]]
但如果你说:

      result.append(currentPath)
它将打印出:

[[], []]
我试着打印出“currentPath”的类型,它已经是一个列表了。既然列表()已经是list类型,为什么还需要它?

,因为:

del currentPath[-1]
换句话说:您正在将
currentPath
对象添加到结果对象中,但随后将对其进行修改。修改将反映在您的
结果中。使用
列表(…)
复制列表。

因此:

del currentPath[-1]

换句话说:您正在将
currentPath
对象添加到结果对象中,但随后将对其进行修改。修改将反映在您的
结果中。使用
list(…)
复制列表。

在您的情况下,如果您这样做

result.append(当前路径)
然后添加对象
currentPath
。实际上,您可以将其视为将指针附加到对象
currentPath
。这意味着
结果
将实际保存
当前路径
,而不是
当前路径
的内容

因此,你什么时候会这样做

del currentPath[-1]
您将更改
currentPath
对象。由于
结果
具有实际的
当前路径
,因此也将修改
结果

但是

当你在做

result.append(列表(当前路径))
您正在追加一个新对象,其中包含
currentPath
的内容。这导致
result
中包含不同的对象,而不是实际的
currentPath
。这是因为
list()
将根据
currentPath
的内容创建一个新列表

因此,当您使用

del currentPath[-1]
结果将不会被修改


要自行验证,您可以检查

id(结果[0])
id(当前路径)

在您的情况下使用
.append(list(currentPath))
.append(currentPath)

result.append(当前路径)
然后添加对象
currentPath
。实际上,您可以将其视为将指针附加到对象
currentPath
。这意味着
结果
将实际保存
当前路径
,而不是
当前路径
的内容

因此,你什么时候会这样做

del currentPath[-1]
您将更改
currentPath
对象。由于
结果
具有实际的
当前路径
,因此也将修改
结果

但是

当你在做

result.append(列表(当前路径))
您正在追加一个新对象,其中包含
currentPath
的内容。这导致
result
中包含不同的对象,而不是实际的
currentPath
。这是因为
list()
将根据
currentPath
的内容创建一个新列表

因此,当您使用

del currentPath[-1]
结果将不会被修改


要自行验证,您可以检查

id(结果[0])
id(当前路径)

当使用
.append(list(currentPath))
.append(currentPath)
时,是为了制作一个浅拷贝。这非常有意义!谢谢你,这是一个肤浅的复制品。这很有道理!非常感谢。