9

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

3
  • In this example, what do you expect the result to be? Commented Dec 18, 2012 at 22:53
  • 2
    Using a NumPy array of dtype object doesn't help to speed things up. They are similar to Python lists, and they are only useful if you need something exposing the same interface as a NumPy array. What is your aim with using this weird data structure? Commented Dec 18, 2012 at 22:57
  • I have tags for my signal in random intervals. Slicing returns me this type (since when slices have different lengths). Commented Dec 18, 2012 at 23:37

1 Answer 1

25

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.