如何将矩阵的列(NOT NUMPY)移至最后一列?

时间:2019-08-24 23:39:59

标签: python list matrix

假设我有一个正则矩阵(表示不是numpy的矩阵),如下所示:

JSON.stringify(userList)

我想将矩阵的第2列移到最后一列:

String

我该怎么做?

3 个答案:

答案 0 :(得分:0)

也许是这样的:

col2 = []
for item in mylist:
    col2.append(item[1])
    del item[1]
for item in col2:
    mylist.insert(4, item)

答案 1 :(得分:0)

这应该可以通过方便的功能完成任务!

请注意,矩阵索引为0!

In [12]: arr = [['A1', 'A2', 'A3', 'A4', 'A5'],
    ...:  [5, 6, 8, 2, 6],
    ...:  [2, 9, 3, 3, 6],
    ...:  [7, 1, 9, 3, 5]]
    ...:

In [15]: def swap_col(arr, fr, to):
    ...:     new_arr = []
    ...:     for row in arr:
    ...:         row[to], row[fr] = row[fr], row[to]
    ...:         new_arr.append(row)
    ...:     return new_arr

In [14]: swap_col(arr, 1, 4)
Out[14]:
[['A1', 'A5', 'A3', 'A4', 'A2'],
 [5, 6, 8, 2, 6],
 [2, 6, 3, 3, 9],
 [7, 5, 9, 3, 1]]

答案 2 :(得分:0)

In [80]: alist = [['A1', 'A2', 'A3', 'A4', 'A5'], 
    ...:  [5, 6, 8, 2, 6], 
    ...:  [2, 9, 3, 3, 6], 
    ...:  [7, 1, 9, 3, 5]]  

有一个众所周知的习惯用法,可以“转换”列表矩阵:

In [86]: x = list(zip(*alist))                                                                               
In [87]: x                                                                                                   
Out[87]: 
[('A1', 5, 2, 7),
 ('A2', 6, 9, 1),
 ('A3', 8, 3, 9),
 ('A4', 2, 3, 3),
 ('A5', 6, 6, 5)]

然后我们可以pop从相关列表中删除,然后将其串联起来:

In [88]: p = x.pop(1)                                                                                        
In [89]: p                                                                                                   
Out[89]: ('A2', 6, 9, 1)
In [90]: x = x+[p]                                                                                           
In [91]: x                                                                                                   
Out[91]: 
[('A1', 5, 2, 7),
 ('A3', 8, 3, 9),
 ('A4', 2, 3, 3),
 ('A5', 6, 6, 5),
 ('A2', 6, 9, 1)]

然后移回:

In [92]: newlist = list(zip(*x))                                                                             
In [93]: newlist                                                                                             
Out[93]: 
[('A1', 'A3', 'A4', 'A5', 'A2'),
 (5, 8, 2, 6, 6),
 (2, 3, 3, 6, 9),
 (7, 9, 3, 5, 1)]

在这种情况下,对象数组版本可以方便使用:

In [96]: arr = np.array(alist, object)                                                                       
In [97]: arr                                                                                                 
Out[97]: 
array([['A1', 'A2', 'A3', 'A4', 'A5'],
       [5, 6, 8, 2, 6],
       [2, 9, 3, 3, 6],
       [7, 1, 9, 3, 5]], dtype=object)
In [102]: np.concatenate([arr[:,[0]],arr[:,2:], arr[:,[1]]], axis=1)                                         
Out[102]: 
array([['A1', 'A3', 'A4', 'A5', 'A2'],
       [5, 8, 2, 6, 6],
       [2, 3, 3, 6, 9],
       [7, 9, 3, 5, 1]], dtype=object)
In [103]: _.tolist()                                                                                         
Out[103]: 
[['A1', 'A3', 'A4', 'A5', 'A2'],
 [5, 8, 2, 6, 6],
 [2, 3, 3, 6, 9],
 [7, 9, 3, 5, 1]]