使用列表推导式实现矩阵转置
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
print([[row[i] for row in matrix]for i in range(4)])
使用内置函数来实现矩阵转置
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
print(list(map(list, zip(*matrix))))
使用二重循环实现矩阵转置
matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
x = []
for i in range(len(matrix[0])):
y = []
for j in range(len(matrix)):
y.append(matrix[j][i])
x.append(y)
print(x)