为了找到一个数字的旋转,我写了如下代码
def rotation(N):
A=[]
for i in range(len(N)):
y=N.pop(0)
N.append(y)
A.append(N)
return A
K=[1,9,7]
r=rotation(K)
print(r)
但它给了我这样的输出:
A=[[1, 9, 7], [1, 9, 7], [1, 9, 7]]
但应该是
A=[[1,9,7],[9,7,1],[7,1,9]]
我不明白为什么会这样 谢谢
最佳答案
使用简单的列表切片:
def rotation(N):
output = []
for i in range(len(N)):
output.append(N[i:] + N[:i])
return output
K=[1,9,7]
r=rotation(K)
print(r)
# [[1, 9, 7], [9, 7, 1], [7, 1, 9]]
关于python - 数字python的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50570334/