1

This is the array I have at hand:

[array([[[ 4, 9,  1, -3],
         [-2, 0, 8, 6],
         [ 1, 3, 7,  9 ],
         [ 2,  5, 0, -7],
         [-1, -6, -5, -8]]]),
 array([[[ 0,  2, -1, 6 ],
         [9,  8,  0,  3],
         [ -1, 2, 5, -4],
         [0,  5, 9, 6],
         [ 6, 2,  9, 4]]]),
 array([[[ 1,  2,  0, 9],
         [3,  4, 8, -1],
         [5,  6,  9,  0],
         [ 7, 8, -3, -],
         [9, 0,  8, -2]]])]

But the goal is obtain arrays A from first columns of nested arrays, B from second columns of nested arrays, Cfrom third columns of nested array etc.

Such that:

A = array([4, -2, 1, 2, -1, 0, 9, -1 ,0, 6, 1, 3, 5, 7, 9])
B = array([9, 0, 3, 5, -6, 2, 8, 2, 5, 2, 2,, 4, 6, 8, 0])

How should I do this?

0

2 Answers 2

1

You can do this with a single hstack() and use squeeze() to remove the extra dimension. With that you can use regular numpy indexing to pull out columns (or anything else you want):

import numpy as np

l = [np.array([[[ 4, 9,  1, -3],
         [-2, 0, 8, 6],
         [ 1, 3, 7,  9 ],
         [ 2,  5, 0, -7],
         [-1, -6, -5, -8]]]),
     np.array([[[ 0,  2, -1, 6 ],
         [9,  8,  0,  3],
         [ -1, 2, 5, -4],
         [0,  5, 9, 6],
         [ 6, 2,  9, 4]]]),
     np.array([[[ 1,  2,  0, 9],
         [3,  4, 8, -1],
         [5,  6,  9,  0],
         [ 7, 8, -3, -1],
         [9, 0,  8, -2]]])]


arr = np.hstack(l).squeeze()

A = arr[:,0]
print(A)
# [ 4 -2  1  2 -1  0  9 -1  0  6  1  3  5  7  9]

B = arr[:,1]
print(B)
#[ 9  0  3  5 -6  2  8  2  5  2  2  4  6  8  0]
# etc...
Sign up to request clarification or add additional context in comments.

1 Comment

That is what I wanted to do... My numpy-fu is not at your level. +1
0

IIUC,

l = [np.array([[[ 4, 9,  1, -3],
         [-2, 0, 8, 6],
         [ 1, 3, 7,  9 ],
         [ 2,  5, 0, -7],
         [-1, -6, -5, -8]]]),
 np.array([[[ 0,  2, -1, 6 ],
         [9,  8,  0,  3],
         [ -1, 2, 5, -4],
         [0,  5, 9, 6],
         [ 6, 2,  9, 4]]]),
 np.array([[[ 1,  2,  0, 9],
         [3,  4, 8, -1],
         [5,  6,  9,  0],
         [ 7, 8, -3, -9],
         [9, 0,  8, -2]]])]

a = np.hstack([i[0][:, 0] for i in l])
b = np.hstack([i[0][:, 1] for i in l])

Output:

array([ 4, -2,  1,  2, -1,  0,  9, -1,  0,  6,  1,  3,  5,  7,  9])
array([ 9,  0,  3,  5, -6,  2,  8,  2,  5,  2,  2,  4,  6,  8,  0])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.