在Python中转置矩阵我试图在Python中创建一个矩阵转置函数。矩阵是一个二维数组,表示为整数列表。例如,下面是一个2X3矩阵(即矩阵的高度为2,宽度为3):A=[[1, 2, 3],
[4, 5, 6]]要将索引中的JTH项转置,应将其转化为JTH索引中的第一项。下面是上面的示例的换位方式:>>> transpose([[1, 2, 3],
[4, 5, 6]])[[1, 4],[2, 5],[3, 6]]>>> transpose([[1, 2],
[3, 4]])[[1, 3],[2, 4]]我该怎么做?
3 回答

墨色风雨
TA贡献1853条经验 获得超6个赞
zip
*
>>> A = [[ 1, 2, 3],[ 4, 5, 6]]>>> zip(*A)[(1, 4), (2, 5), (3, 6)]>>> lis = [[1,2,3], ... [4,5,6],... [7,8,9]]>>> zip(*lis)[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> [list(x) for x in zip(*lis)][[1, 4, 7], [2, 5, 8], [3, 6, 9]]#or>>> map(list, zip(*lis))[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

阿波罗的战车
TA贡献1862条经验 获得超6个赞
import numpy as np a = np.array([(1,2,3), (4,5,6)])b = a.transpose()

繁星coding
TA贡献1797条经验 获得超4个赞
return [[ m[row][col] for col in range(0,width) ] for row in range(0,height) ]
[[1,2,3],[4,5,6],[7,8,9]]
[[1,4,7],[2,5,8],[3,6,9]]
return [[ m[row][col] for row in range(0,height) ] for col in range(0,width) ]
添加回答
举报
0/150
提交
取消