阵列就地旋转器返回错误值
Array in-place rotater returning wrong values
我想定义一个函数来原地旋转矩阵 90 度
def rotate_matrix(matrix):
for i in range(len(matrix)//2):
for j in range(i, len(matrix)-i-1):
matrix[~j][i], matrix[i][j], matrix[j][~i], matrix[~i][~j] = matrix[i][j], matrix[j][~i], matrix[~i][~j], matrix[~j][i]
return matrix
插入时:
[
[a, b],
[c, d]
]
它returns:
[
[b, d],
[a, c]
]
而不是:
[
[c, a],
[d, b]
]
我不确定为什么。
你走对了!您的代码执行 逆时针旋转 而不是 顺时针 。
要解决它,您必须对分配逻辑进行一些小的更改:
def rotate_matrix(matrix):
for i in range(len(matrix)//2):
for j in range(i, len(matrix)-i-1):
matrix[~j][i], matrix[i][j], matrix[j][~i], matrix[~i][~j] = \
matrix[~i][~j], matrix[~j][i], matrix[i][j], matrix[j][~i]
return matrix
满足您的需求。
但是,我会使用 numpy,因为它有一个 built in method 用于旋转矩阵:
import numpy as np
mat = np.array([['a','b'],
['c','d']])
def rotate_matrix(matrix):
return np.rot90(matrix, 3) // * SEE NOTE
print(rotate_matrix(mat))
Returns:
[['c' 'a']
['d' 'b']]
注意: rot90 方法提供逆时针 旋转。由于您请求 顺时针 旋转,因此您必须指定参数 3 以指定实现顺时针旋转应进行的逆时针旋转量。
这就是你的问题的解决方法,你必须正确使用赋值
试着看看你正在做的作业,
就索引而言:(0,0) --> (0,1) , (0,1) --> (1,1), (1,0) -->(0,0 ) 和
(1,1) --> (1,0) 这是错误的。
这就是你得到错误解决方案的原因
你应该做的是将索引匹配为
(0,0) -->(0,1) , (0,1)-->(0,0) , (1,1)-->(0,1),(1, 0)-->(1,1)
下面是正确的解决方案。
def rotate_matrix(matrix):
for i in range(len(matrix)//2):
for j in range(i, len(matrix)-i-1):
matrix[i][j], matrix[~i][j], matrix[i][~j], matrix[~i][~j]= matrix[~i][j],matrix[i][~j],matrix[i][j],matrix[~i][~j]
return matrix
a = [
['a','b'],
['c', 'd']
]
print(rotate_matrix(a))
# output [['c', 'a'], ['b', 'd']]
这是您要解决的问题的解决方案,即矩阵的旋转
在 90 度
# Python 旋转矩阵的程序
# Function to rotate a matrix
def rotateMatrix(mat):
if not len(mat):
return
"""
top : starting row index
bottom : ending row index
left : starting column index
right : ending column index
"""
top = 0
bottom = len(mat)-1
left = 0
right = len(mat[0])-1
while left < right and top < bottom:
# Store the first element of next row,
# this element will replace first element of
# current row
prev = mat[top+1][left]
# Move elements of top row one step right
for i in range(left, right+1):
curr = mat[top][i]
mat[top][i] = prev
prev = curr
top += 1
# Move elements of rightmost column one step downwards
for i in range(top, bottom+1):
curr = mat[i][right]
mat[i][right] = prev
prev = curr
right -= 1
# Move elements of bottom row one step left
for i in range(right, left-1, -1):
curr = mat[bottom][i]
mat[bottom][i] = prev
prev = curr
bottom -= 1
# Move elements of leftmost column one step upwards
for i in range(bottom, top-1, -1):
curr = mat[i][left]
mat[i][left] = prev
prev = curr
left += 1
return mat
# Utility Function
def printMatrix(mat):
for row in mat:
print row
# Test case 1
matrix = [
['a','b'],
['c', 'd']
]
matrix = rotateMatrix(matrix)
# Print modified matrix
printMatrix(matrix)
# output [['c', 'a'], ['b', 'd']]
ps 来自 geeksforgeets
的第二个解决方案
我想定义一个函数来原地旋转矩阵 90 度
def rotate_matrix(matrix):
for i in range(len(matrix)//2):
for j in range(i, len(matrix)-i-1):
matrix[~j][i], matrix[i][j], matrix[j][~i], matrix[~i][~j] = matrix[i][j], matrix[j][~i], matrix[~i][~j], matrix[~j][i]
return matrix
插入时:
[
[a, b],
[c, d]
]
它returns:
[
[b, d],
[a, c]
]
而不是:
[
[c, a],
[d, b]
]
我不确定为什么。
你走对了!您的代码执行 逆时针旋转 而不是 顺时针 。
要解决它,您必须对分配逻辑进行一些小的更改:
def rotate_matrix(matrix):
for i in range(len(matrix)//2):
for j in range(i, len(matrix)-i-1):
matrix[~j][i], matrix[i][j], matrix[j][~i], matrix[~i][~j] = \
matrix[~i][~j], matrix[~j][i], matrix[i][j], matrix[j][~i]
return matrix
满足您的需求。
但是,我会使用 numpy,因为它有一个 built in method 用于旋转矩阵:
import numpy as np
mat = np.array([['a','b'],
['c','d']])
def rotate_matrix(matrix):
return np.rot90(matrix, 3) // * SEE NOTE
print(rotate_matrix(mat))
Returns:
[['c' 'a']
['d' 'b']]
注意: rot90 方法提供逆时针 旋转。由于您请求 顺时针 旋转,因此您必须指定参数 3 以指定实现顺时针旋转应进行的逆时针旋转量。
这就是你的问题的解决方法,你必须正确使用赋值
试着看看你正在做的作业,
就索引而言:(0,0) --> (0,1) , (0,1) --> (1,1), (1,0) -->(0,0 ) 和 (1,1) --> (1,0) 这是错误的。
这就是你得到错误解决方案的原因
你应该做的是将索引匹配为
(0,0) -->(0,1) , (0,1)-->(0,0) , (1,1)-->(0,1),(1, 0)-->(1,1)
下面是正确的解决方案。
def rotate_matrix(matrix):
for i in range(len(matrix)//2):
for j in range(i, len(matrix)-i-1):
matrix[i][j], matrix[~i][j], matrix[i][~j], matrix[~i][~j]= matrix[~i][j],matrix[i][~j],matrix[i][j],matrix[~i][~j]
return matrix
a = [
['a','b'],
['c', 'd']
]
print(rotate_matrix(a))
# output [['c', 'a'], ['b', 'd']]
这是您要解决的问题的解决方案,即矩阵的旋转 在 90 度 # Python 旋转矩阵的程序
# Function to rotate a matrix
def rotateMatrix(mat):
if not len(mat):
return
"""
top : starting row index
bottom : ending row index
left : starting column index
right : ending column index
"""
top = 0
bottom = len(mat)-1
left = 0
right = len(mat[0])-1
while left < right and top < bottom:
# Store the first element of next row,
# this element will replace first element of
# current row
prev = mat[top+1][left]
# Move elements of top row one step right
for i in range(left, right+1):
curr = mat[top][i]
mat[top][i] = prev
prev = curr
top += 1
# Move elements of rightmost column one step downwards
for i in range(top, bottom+1):
curr = mat[i][right]
mat[i][right] = prev
prev = curr
right -= 1
# Move elements of bottom row one step left
for i in range(right, left-1, -1):
curr = mat[bottom][i]
mat[bottom][i] = prev
prev = curr
bottom -= 1
# Move elements of leftmost column one step upwards
for i in range(bottom, top-1, -1):
curr = mat[i][left]
mat[i][left] = prev
prev = curr
left += 1
return mat
# Utility Function
def printMatrix(mat):
for row in mat:
print row
# Test case 1
matrix = [
['a','b'],
['c', 'd']
]
matrix = rotateMatrix(matrix)
# Print modified matrix
printMatrix(matrix)
# output [['c', 'a'], ['b', 'd']]
ps 来自 geeksforgeets
的第二个解决方案