简体   繁体   中英

How to cast a list into an array with specific ordering of the elements in the array

If I have a list:

lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

I would like to cast the above list into an array with the following arrangements of the elements:

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

How do I do this or what is the best way to do this? Many thanks.

I have done this in a crude way below where I will just get all the sub-matrix and then concatenate all of them at the end:

np.array(results[arr.shape[0]*arr.shape[1]*0:arr.shape[0]*arr.shape[1]*1]).reshape(arr.shape[0], arr.shape[1])
array([[1, 2, 3],
       [4, 5, 6]])

np.array(results[arr.shape[0]*arr.shape[1]*1:arr.shape[0]*arr.shape[1]*2]).reshape(arr.shape[0], arr.shape[1])
array([[ 7,  8,  9],
       [ 10, 11, 12]])

etc,

But I will need a more generalized way of doing this (if there is one) as I will need to do this for an array of any size.

You could use the reshape function from numpy, with a bit of indexing :

a = np.arange(24)
>>> 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])

Using reshape and a bit of indexing :

a = a.reshape((8,3))
idx = np.arange(2)
idx = np.concatenate((idx,idx+4))
idx = np.ravel([idx,idx+2],'F')
b = a[idx,:].reshape((4,6))

Ouptut :

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

Here the tuple (4,6) passed to reshape indicates that you want your array to be 2 dimensional, and have 4 arrays of 6 elements. Those values can be computed. Then we compute the index to set the correct order of the data. Obvisouly, this a complicated bit here. As I'm not sure what you mean by "any size of data", its difficult for me to give you a agnostic way to compute that index.

Obviously, if you are using a list and not an np.array, you might have to convert the list first, for example by using np.array(your_list) .

Edit :

I'm not sure if this exactly what you are after, but this should work for any array evenly divisible by 6 :

def custom_order(size):
    a = np.arange(size)
    a = a.reshape((size//3,3))
    idx = np.arange(2)
    idx = np.concatenate([idx+4*i for i in range(0,size//(6*2))])
    idx = np.ravel([idx,idx+2],'F')
    b = a[idx,:].reshape((size//6,6))
    return b
>>> custom_order(48)
array([[ 0,  1,  2,  6,  7,  8],
       [ 3,  4,  5,  9, 10, 11],
       [12, 13, 14, 18, 19, 20],
       [15, 16, 17, 21, 22, 23],
       [24, 25, 26, 30, 31, 32],
       [27, 28, 29, 33, 34, 35],
       [36, 37, 38, 42, 43, 44],
       [39, 40, 41, 45, 46, 47]])

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