Python Square Matrix Transpose Function
def square_matrix_tranpose(matrix):
for c in range(len(matrix)-1):
for r in range(c+1,len(matrix)):
matrix[r][c],matrix[c][r] = matrix[c][r],matrix[r][c]
return matrix
# Test cases
print(square_matrix_tranpose([[1,2,3],[4,5,6],[7,8,9]])) # [[1,4,7],[2,5,8],[3,6,9]]
print(square_matrix_tranpose([[1,2],[3,4]])) # [[1,3],[2,4]]
print(square_matrix_tranpose([[1,0,0],[0,1,0],[0,0,1]])) # [[1,0,0],[0,1,0],[0,0,1]]
print(square_matrix_tranpose([[1,2],[3,4],[5,6]])) # [[1,3,5],[2,4,6]]
print(square_matrix_tranpose([[1]])) # [[1]]
原文地址: https://www.cveoy.top/t/topic/nXgx 著作权归作者所有。请勿转载和采集!