逆时针旋转矩阵:先转置,再上下翻转。
顺时针旋转矩阵:先上下翻转,再转置。
a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
b = list(map(list, zip(*a)))[::-1]
’‘’
zip(*a)求出来矩阵旋转,但是这是一个元组,
((3, 6, 9),
(2, 5, 8),
(1, 4, 7))通过map(list,*a)将每个元素变成list,
[(3, 6, 9),
(2, 5, 8),
(1, 4, 7)]
然后再套一层list将外层的元组变长list
‘’‘
print(b)
‘’‘
[[3, 6, 9],
[2, 5, 8],
[1, 4, 7]]
’‘’
a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
b = list(map(list, zip(*a[::-1])))
print(b)
’‘’
[[7, 4, 1],
[8, 5, 2],
[9, 6, 3]]
‘’‘