核心思路

标准解法(解法1)
python
def transpose_matrix(a: list[list[int|float]]) -> list[list[int|float]]:
origin_row = len(a)
t_matrix_row = len(a[0])
t_matrix = []
for i in range(t_matrix_row):
new_row = []
for j in range(origin_row):
new_row.append(a[j][i])
t_matrix.append(new_row)
return t_matrix
if __name__ == "__main__":
a = [[1,2,3],[4,5,6]]
print(transpose_matrix(a))
zip函数解法
python
def transpose_matrix(a: list[list[int|float]]) -> list[list[int|float]]:
return [list(new_row) for new_row in zip(*a)]
if __name__ == "__main__":
a = [[1,2,3],[4,5,6]]
print(transpose_matrix(a))