>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""
编辑:最好不要使用numpy。
>>> def rotate_matrix( k: List[List[int]]):
"""
For example, if I have:
m = [[1,2,3],
[2,3,3],
[5,4,3]]
rotate_matrix(m) should give me [[3,3,3],[2,3,4],[1,2,5]].
"""
编辑:最好不要使用numpy。
以下是不使用Numpy的纯Python代码实现逆时针矩阵旋转的一行代码:
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
如果你想在一个函数中实现这个功能,则
def rotate_matrix( m ):
return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
无论如何,结果都是
m = [ [1,2,3], [2,3,3], [5,4,3]]
是什么
[[3, 3, 3], [2, 3, 4], [1, 2, 5]]
另外,如果您想要常规的转置,那么简单的一行纯Python版本如下:
[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]
import numpy as np
m = np.array([[1,2,3],
[2,3,3],
[5,4,3]])
def rotate_matrix(mat):
return np.rot90(mat)
#解决方案1
print(list(list(x) for x in zip(*m))[::-1])
print([[x[i] for x in m] for i in range(len(m))][::-1])
#solution3
r = []
i = 0
for row in m:
listToAdd = [item[i] for item in m]
r.append(listToAdd)
i +=1
print(r[::-1])
list(zip(*m))[::-1]
或 [*zip(*m)][::-1]
。 - ggorlen*
解包 m,然后再用 zip
打包回去? - radoslav006zip
的参数是不同的列表,zip
的输出是每个列表的第n个元素组合在一起。 - undefineddef rotate_90_degree_anticlckwise(matrix):
new_matrix = []
for i in range(len(matrix[0]), 0, -1):
new_matrix.append(list(map(lambda x: x[i-1], matrix)))
return new_matrix
def rotate_90_degree_clckwise(matrix):
new_matrix = []
for i in range(len(matrix[0])):
li = list(map(lambda x: x[i], matrix))
li.reverse()
new_matrix.append(li)
return new_matrix
def rotate_90_degree_clckwise_by_zip_method(matrix):
return list(list(x)[::-1] for x in zip(*matrix))
看一下这段代码。这是使用scratch将任何矩阵旋转90度的方法。
[这是将矩阵旋转90度的代码][1]
n = input("enter input:")
l =[]
while (n !=""):
l.append(n.split(" "))
n = input("enter input:")
l1=[]
for i in range(len(l[0])):
l1.append([])
for j in range (len(l1)):
for p in range(len(l)):
l1[j].append(l[p][-(j+1)])
for s in l1:
print(*s)
'''
[1]: https://istack.dev59.com/c8jHC.webp
def rotated_array(array):
new_array = np.zeros([array.shape[0], array.shape[1]])
for i in range(array.shape[0]):
for j in range(array.shape[1]):
t = array.shape[0]-j-1 # To Start with last row and first column
new_array[i, j] = array[t, i]
return new_array
将矩阵逆时针旋转90度(原地操作)
def rotate(matrix):
for i in range(len(matrix) - 1, -1, -1):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
matrix.reverse()
原地顺时针旋转矩阵90度
def rotate(matrix):
matrix.reverse()
for i in range(len(matrix)):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
def left_rotate(mat):
if not mat: return []
colcount = len(mat[0])
# Initialize empty lists
new = [list() for _ in range(colcount)]
# Go through the rows, append elements one by one
for row in mat:
for j in range(colcount):
new[j].append(row[colcount - 1 - j])
return new
例子:
>>> print(left_rotate([[1, 2], [3, 4], [5, 6]]))
[[2, 4, 6], [1, 3, 5]]
如果您正在使用方阵(行数等于列数),那么可以使用以下代码:
list(zip(*reversed(a))) # 90 degrees clockwise
list(zip(*reversed(a)))[::-1] # 90 degrees counter-clockwise
reversed
,使其变为:list(zip(*a))[::-1] # 逆时针旋转90度
。 - craigpastro只需将矩阵垂直翻转,然后将右上角三角形与左下角三角形交换
def rotate_matrix_ccw(mat):
if mat is None:
return None
n = len(mat)
if n == 1:
return mat
for i in range(n):
if len(mat[i]) != n:
raise Exception("Matrix must be square")
# flip the matrix vertically
for j in range(n // 2):
for i in range(n):
mat[i][j], mat[i][n - 1 - j] = mat[i][n - 1 - j], mat[i][j]
# switch the upper-right triangle with the lower-left triangle
for i in range(n):
for j in range(i):
mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
return mat