顺时针打印矩阵
2018-05-10 本文已影响0人
流光汐舞
#递归
def snail(array):
return list(array[0]) + snail(zip(*array[1:])[::-1]) if array else []
#非递归
def snail(array):
a = []
while array:
a.extend(list(array.pop(0)))
array = zip(*array)
array.reverse()
return a
如:
array=[
[1, 2, 3,],
[4, 5, 6,],
[7, 8, 9,]
]
result=[1, 2, 3, 6, 9, 8, 7, 4, 5]
#矩阵转置
def snail_reverse(array):
return map(list,zip(*array))
array=[
[1, 2, 3,],
[4, 5, 6,],
[7, 8, 9,]
]
result=[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]