gpt4 book ai didi

python - 矩阵顺时针旋转 90 度

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:36 26 4
gpt4 key购买 nike

<分区>

我正在解决矩阵旋转 90 度的问题。在这个问题中,我正在使用一个列表 k,其中填充了用户传递的确切度数的 0。

我试过下面的代码:

def rotate(m):
k=[]
f=[]
print(m)
for i in range(0,len(m)):
f.append(0)
for i in range(0,len(m)):
k.append(f)
print(k)
for i in range(0,len(m)):
for j in range(0,len(m)):
print("REPLACING POSITION:",i,j )
t=m[i][j]
k[j][len(m)-i-1]=t
return (k)
print(rotate([[1,2],[3,4]]))

我期望输出:

[[1, 2], [3, 4]]
[[0, 0], [0, 0]]
REPLACING POSITION: 0 0
REPLACING POSITION: 0 1
REPLACING POSITION: 1 0
REPLACING POSITION: 1 1
[[3, 1], [4, 2]]

我正在获取输出:

[[1, 2], [3, 4]]
[[0, 0], [0, 0]]
REPLACING POSITION: 0 0
REPLACING POSITION: 0 1
REPLACING POSITION: 1 0
REPLACING POSITION: 1 1
[[4, 2], [4, 2]]

为什么最后一行不断重复?请帮忙。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com