【发布时间】: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