I'm taking ndarray slices with different length and I want my result to be flat. For example:
a = np.array(((np.array((1,2)), np.array((1,2,3))), (np.array((1,2)), np.array((1,2,3,4,5,6,7,8)))))
Is there any straight way to make this array flat by using numpy functionalities (without loop)?
By using ndarray. flatten() function we can flatten a matrix to one dimension in python. order:'C' means to flatten in row-major. 'F' means to flatten in column-major.
ndarray. flatten. Return a copy of the array collapsed into one dimension.
You could try flattening it and then using hstack, which stacks the array in sequence horizontally.
>>> a = np.array(((np.array((1,2)), np.array((1,2,3))), (np.array((1,2)), np.array((1,2,3,4,5,6,7,8)))))
>>> np.hstack(a.flatten())
array([1, 2, 1, 2, 3, 1, 2, 1, 2, 3, 4, 5, 6, 7, 8])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With