def perm(arr, low, high): #arr = [0,1,1], low = 0, high = 3
if low == high:
print (arr)
else:
for i in range(low, high):
arr[low], arr[i] = arr[i], arr[low]
perm(arr, low + 1, high)
arr[low], arr[i] = arr[i], arr[low]
这是一种置换方法,我用于遍历某个列表的所有可能序列,例如,如果我输入[0,1,1],则输出应为[0,1,1],[1,0 ,1]和[1,1,0]。而且效果很好。但是当我尝试:
result = []
def perm(arr, low, high): #arr = [0,1,1], low = 0, high = 3
if low == high:
print(arr)
result.append[arr]
else:
for i in range(low, high):
arr[low], arr[i] = arr[i], arr[low]
perm(arr, low + 1, high)
arr[low], arr[i] = arr[i], arr[low]
只能正确打印输出,但结果列表为:
>>>result:
>>>[[0,1,1],[0,1,1],[0,1,1]]
我应该如何纠正它,以便结果列表中的元素与打印的内容相同?