如何在python中更改列表数组的数组形状?

时间:2017-12-05 19:48:17

标签: python arrays reshape

我有(808,1,22,2000)列表数组从json文件加载 它有808个(22,2000)个数组 所以我想把它变成(22,2000,808) 你能告诉我怎么做吗?

1 个答案:

答案 0 :(得分:0)

虽然这里存在歧义,因为不清楚为什么要改变形状,如果我是对的,numpy.reshape应该是你的答案。 看看这个例子:

>> a = np.array([[[[0,1,2,3],[4,5,6,7]]],[[[8,9,10,11],[12,13,14,15]]],[[[16,17,18,19],[20,21,22,23]]]])
>> a
array([[[[ 0,  1,  2,  3],
     [ 4,  5,  6,  7]]],

   [[[ 8,  9, 10, 11],
     [12, 13, 14, 15]]],

   [[[16, 17, 18, 19],
     [20, 21, 22, 23]]]])

>> a.shape
(3, 1, 2, 4)

>> b = a.reshape((a.shape[2],a.shape[3],a.shape[0]))
>> b
array([[[ 0,  1,  2],
    [ 3,  4,  5],
    [ 6,  7,  8],
    [ 9, 10, 11]],

   [[12, 13, 14],
    [15, 16, 17],
    [18, 19, 20],
    [21, 22, 23]]])

>> b.shape
(2, 4, 3)