это решение вашей проблемы, вы должны использовать собственно значение присваивания
попытайтесь увидеть выполняемое вами задание,
по показателю индекса: (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