料青山看我應(yīng)如是
2023-04-11 15:44:38
我想更改矩陣的軸x和軸。y例如,我想將每個(gè)嵌套數(shù)組的第一個(gè)元素存儲在第一行,將每個(gè)嵌套數(shù)組的第二個(gè)元素存儲在第二行等...例如:list = [[1,2,3,4,5,6]
[7,8,9,10,11,12]
[13,14,15,16,17,18]
[19,20,21,22,23,24]]我想改成這樣:new list = [[1,7,13,19]
[2,8,14,20]
[3,9,15,21]
[4,10,16,22]
[5,11,17,23]
[6,12,18,24]]注意:這不是旋轉(zhuǎn)
1 回答

蕭十郎
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超13個(gè)贊
使用
numpy.ndarray.T
,這與numpy.transpose
import numpy as np
data = [[1,2,3,4,5,6],
? ? ? ? [7,8,9,10,11,12],
? ? ? ? [13,14,15,16,17,18],
? ? ? ? [19,20,21,22,23,24]]
# convert the list of lists to an array
data = np.array(data)
# transpose the array
data_t = data.T
# print(data_t)
array([[ 1,? 7, 13, 19],
? ? ? ?[ 2,? 8, 14, 20],
? ? ? ?[ 3,? 9, 15, 21],
? ? ? ?[ 4, 10, 16, 22],
? ? ? ?[ 5, 11, 17, 23],
? ? ? ?[ 6, 12, 18, 24]])
添加回答
舉報(bào)
0/150
提交
取消