用于移位数组元素的Python程序
我试图创建一个python程序来洗牌数组,这样水平行和垂直行就不会有重复编号用于移位数组元素的Python程序,python,arrays,python-3.x,Python,Arrays,Python 3.x,我试图创建一个python程序来洗牌数组,这样水平行和垂直行就不会有重复编号 Input: [1,2,3,4] Output: 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 我的程序正确地计算每个元素的移位,但当它将列表附加到输出列表时,输出列表中只有列表中最后一项的重复副本 def numbers(list_of_numbers): finalValues = [list_of_numbers] #print(list_of_numbers)
Input: [1,2,3,4]
Output:
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
我的程序正确地计算每个元素的移位,但当它将列表附加到输出列表时,输出列表中只有列表中最后一项的重复副本
def numbers(list_of_numbers):
finalValues = [list_of_numbers]
#print(list_of_numbers)
for i in range(1,len(list_of_numbers)):
print("Last array of final: ", finalValues[-1])
tempArray = finalValues[-1]
print("Temp Array: ",tempArray)
temp = tempArray[0]
for j in range(0,len(list_of_numbers)-1):
tempArray[j] = tempArray[j+1]
tempArray[-1] = temp
finalValues.append(tempArray)
print("Final Values: ",finalValues)
return finalValues
numbers([1,2,3,4])
程序输出
正确输出
问题来自于线路:
tempArray = finalValues[-1]
您不会创建上一个列表的副本,而只创建一个新名称来引用它。在此之后,您对tempArray
所做的所有更改实际上都是对此列表的更改,当您最终执行此操作时:
finalValues.append(tempArray)
您只需在finalValues
中向同一列表添加另一个引用
最后,finalValues
包含对同一列表的4个引用,您可以使用finalValues[0]
,finalValues[1]
访问这些引用
您需要的是通过复制上一个列表来创建一个新列表。一种方法是使用切片:
tempArray = finalValues[-1][:]
您可以找到其他方法在中关闭或复制列表
因此,完整的代码给出了预期的输出:
Last array of final: [1, 2, 3, 4]
Temp Array: [1, 2, 3, 4]
Final Values: [[1, 2, 3, 4], [2, 3, 4, 1]]
Last array of final: [2, 3, 4, 1]
Temp Array: [2, 3, 4, 1]
Final Values: [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2]]
Last array of final: [3, 4, 1, 2]
Temp Array: [3, 4, 1, 2]
Final Values: [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]
[[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]
Thierry提供了一个非常全面的解释,解释了为什么您的代码不能按预期工作。因此,这是对你的问题的最佳答案。我添加了我的答案,作为你的一个例子,你可以用一种不太复杂的方式编写它 创建二维列表,第一个索引为数字列表。对于每个迭代,获取temp的最后一个索引,并从索引1到末尾进行切片,然后添加索引0 然后返回列表
def编号(编号列表):
temp=[编号列表]
对于范围内的(1,len(编号列表)):
临时附加(临时[-1][1:][temp[-1][0:1])
返回温度
打印(数字([1,2,3,4]))
输出
[[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]
问题在于数组的
浅分配。您应该进行deep
copy,以真正克隆阵列,使其独立
我用你自己的密码做的。您的代码有一些更改:
导入已添加到第一行的副本
copy.deepcopy
函数代替=
(简单赋值)的三种用法
导入副本
def编号(编号列表):
finalValues=copy.deepcopy([数字列表])
#打印(编号列表)
对于范围内的i(1,len(编号列表)):
打印(“最终值的最后一个数组:”,最终值[-1])
tempArray=copy.deepcopy(最终值[-1])
打印(“临时数组:”,临时数组)
temp=tempArray[0]
对于范围内的j(0,len(编号列表)-1):
tempArray[j]=tempArray[j+1]
临时数组[-1]=临时
finalValues.append(copy.deepcopy(tempArray))
打印(“最终值:”,最终值)
返回最终值
数字([1,2,3,4])
程序输出
程序输出
您将输入列表包装在一个新列表中,然后获取新列表的最后一个元素,即整个iput列表。然后继续编辑相同的列表
Last array of final: [1, 2, 3, 4]
Temp Array: [1, 2, 3, 4]
Final Values: [[1, 2, 3, 4], [2, 3, 4, 1]]
Last array of final: [2, 3, 4, 1]
Temp Array: [2, 3, 4, 1]
Final Values: [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2]]
Last array of final: [3, 4, 1, 2]
Temp Array: [3, 4, 1, 2]
Final Values: [[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]
[[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]
[[1, 2, 3, 4], [2, 3, 4, 1], [3, 4, 1, 2], [4, 1, 2, 3]]
[[4, 1, 2, 3], [4, 1, 2, 3], [4, 1, 2, 3], [4, 1, 2, 3]]
[[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3]]