重塑python中的怪异列表

时间:2018-07-17 03:04:11

标签: python arrays list numpy

在Python中,我有一个像这样的列表:

array = 
[[[0,0,1,0],[0,0,0,1],[1,0,0,0],[0,1,0,0]],
[[0,1,0],[1,0,0],[0,0,1],[0,0,1]],
[[0],[1],[0],[1]],
[[1],[1],[0],[1]]]

我想将其重塑为:

array = 
[[[0,0,1,0],[0,1,0],[0],[1]],
[[0,0,0,0],[1,0,0],[1],[1]],
[[1,0,0,0],[0,0,1],[0],[0]],
[[0,1,0,0],[0,0,1],[0],[1]]]

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

IIUC,给定

array = [[[0,0,1,0],[0,0,0,1],[1,0,0,0],[0,1,0,0]],
        [[0,1,0],[1,0,0],[0,0,1],[0,0,1]],
        [[0],[1],[0],[1]],
        [[1],[1],[0],[1]]]

import numpy as np
>>> np.array(array).T

array([[list([0, 0, 1, 0]), list([0, 1, 0]), list([0]), list([1])],
       [list([0, 0, 0, 1]), list([1, 0, 0]), list([1]), list([1])],
       [list([1, 0, 0, 0]), list([0, 0, 1]), list([0]), list([0])],
       [list([0, 1, 0, 0]), list([0, 0, 1]), list([1]), list([1])]],