【问题标题】:Python: append() weird behavior during recursionPython:递归期间的 append() 奇怪行为
【发布时间】:2020-11-11 19:48:39
【问题描述】:

我正在编写以下程序来从给定列表中生成所有可能的排列。

def permute(self, nums: List[int]) -> List[List[int]]:

        def __permute(nums, n, chosen, perms):
            if len(perms) == n:
                print(perms)
            else:
                for i in range(n):
                    if chosen[i]:
                        continue
                    else:
                        chosen[i] = True
                        perms.append(nums[i])
                        __permute(nums, n, chosen, perms)
                        perms.pop()
                        chosen[i] = False
            
        n = len(nums)
        __permute(nums, n, [False]*n, [])

例如:

Input: [1,2,3]
Output:
  [1,2,3]
  [1,3,2]
  [2,1,3]
  [2,3,1]
  [3,1,2]
  [3,2,1]

现在,我想将所有排列添加到列表中并返回:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

这是我的代码:

def permute(self, nums: List[int]) -> List[List[int]]:

        def __permute(nums, n, chosen, perms,res):
            if len(perms) == n:
                res.append(perms)
            else:
                for i in range(n):
                    if chosen[i]:
                        continue
                    else:
                        chosen[i] = True
                        perms.append(nums[i])
                        __permute(nums, n, chosen, perms, res)
                        perms.pop()
                        chosen[i] = False
            
        n = len(nums)
        res = []
        __permute(nums, n, [False]*n, [], res)
        return res

问题是,输出中充满了空列表。

Input: [1,2,3]
Output:
[
  [],
  [],
  [],
  [],
  [],
  []
]

我通过将res.append(perms) 替换为res.append(perms[:]) 修复了该错误,但我不明白它为什么会起作用。

我还打印了每个 perms 的 id() 并注意到一些奇怪的东西:

>>> print(id(perms))
>>>
140093677843328
140093677843328
140093677843328
140093677843328
140093677843328
140093677843328
>>> print(id(perms[:]))
>>>
140689610181440
140689610145600
140689610145600
140689610145600
140689610145600
140689610145600

有人对此行为有解释吗?

【问题讨论】:

  • 因为您总是使用同一个perms 列表。你的算法清除了那个列表,perms.pop()...所以最后,你会得到一堆对同一个空列表的引用。
  • res.append(perms) 时,列表perms 作为参考。这个perms 之后被perms.pop() 变为空。结果返回的值都是空的。
  • 啊!现在知道了,非常感谢。

标签: python python-3.x list recursion append


【解决方案1】:

perms 每次都是同一个对象,但是当您执行perms[:] 时,您正在创建perms 的副本(这是与perms 分开的对象)。

res (list) 存储了对perms 的引用,因此如果更改perms 相同的内容将反映在res 列表中。

但是执行res[:] 会给你一个perms 的副本(这是一个不同的对象),如上所述。

【讨论】:

    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 2013-09-04
    • 1970-01-01
    • 2016-11-06
    • 2016-11-07
    • 2015-01-31
    • 2018-11-13
    • 2020-02-29
    相关资源
    最近更新 更多