我的代码适用于2d和3d类型列表数组,但不适用于numpy.ndarray类型的3d数组:
def rotate(lst):
si = 0
ei = len(lst)-1
sj = 0
ej = len(lst[0])-1
ix = 0
jx = 1
i = 0
j = 0
pre = lst[0][0]
for x in range(1, (len(lst[0])) * (len(lst))):
if i == ei and ix == 1: # V
ix = 0
jx = -1
ej -= 1
elif j == ej and jx == 1: # >
ix = 1
jx = 0
si += 1
elif i == si and ix == -1: # ^
ix = 0
jx = 1
sj += 1
elif j == sj and jx == -1: # <
ix = -1
jx = 0
ei -= 1
print(pre,end=',')
t = lst[i + ix][j + jx]
lst[i + ix][j + jx] = pre
pre = t
i += ix
j += jx
lst[0][0] = pre
return lst
print(rotate(msg))
提供2D输入时
> msg = numpy.array([
> [1,2,3],
> [8,9,4],
> [7,6,5],
> ])
op:
> 1,2,3,4,5,6,7,8,
>
> [[9 1 2] [7 8 3] [6 5 4]]
在输入3d时
> msg = numpy.array([
> [[1, 1], [2, 2], [3, 3]],
> [[8, 8], [9, 9], [4, 4]],
> [[7, 7], [6, 6], [5, 5]],
> ])
op:
> [2 2],[2 2],[2 2],[2 2],[2 2],[2 2],[2 2],[2 2],
>
> [[[2 2] [2 2] [2 2]]
> [[2 2] [2 2] [2 2]]
> [[2 2] [2 2] [2 2]]]
我想要以下操作:
> [[[9 9] [1 1] [2 2]]
> [[7 7] [8 8] [3 3]]
> [[6 6] [5 5] [4 4]]]
在检查我发现执行lst[i + ix][j + jx] = pre
后发现t的值被更改了。