我有一个列表,开头是
value=[1,2,3]
out_list=[value]
while(i<2*len(nums)):
temp_value=temp[locator]
temp[locator]=temp[locator+1]
temp[locator+1]=temp_value
print(temp, end=" ")
output_list+=[temp]
print(output_list)
i+=1
然后我看一下我用temp得到的值是一些正常值,如图所示:
[1, 3, 2]
[3, 2, 1]
[3, 1, 2]
[2, 3, 1]
[2, 1, 3]
这应该意味着我的输出列表也很好,对吗?不,这是我的最终输出
[[1, 3, 2], [1, 3, 2], [1, 3, 2]]
[[1, 3, 2], [1, 3, 2], [1, 3, 2], [3, 2, 1]]
[[1, 3, 2], [1, 3, 2], [1, 3, 2], [3, 1, 2], [3, 1, 2]]
[[1, 3, 2], [1, 3, 2], [1, 3, 2], [3, 1, 2], [3, 1, 2], [2, 3, 1]]
[[1, 3, 2], [1, 3, 2], [1, 3, 2], [3, 1, 2], [3, 1, 2], [2, 1, 3], [2, 1, 3]]
你只想让名单上的人加入,对吗
比如:
如果是这种情况,请从[temp]中删除[]
更新: 试试这个
我发布了一个回溯解决方案-因为你的标签中有回溯。 这将为您提供列表的所有排列->;嵌套列表作为输出
permute([1,2,3])
将产生相关问题 更多 >
编程相关推荐