简体   繁体   中英

How to change array shape of list array in python?

I have (808,1,22,2000) list array which load from json file.
It has 808 number of (22,2000) arrays.
so i want to make this to (22,2000,808).
Can you let me know how to do it?

Although there is an ambiguity here because it's not clear why you want to change the shape, if I'm right, numpy.reshape should be your answer. Look at this example:

>> 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM