0

I want to split at array multiple times at various indices.

Example:

y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

I want to split this array at indices 2, 4, 7 to generate the following result:

res = [[[0, 1], [2, 3, 4, 5, 6, 7, 8, 9]],
       [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9]],
       [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9]]]

I tried using numpy.split but it seems to only be able to split once, not multiple times to generate the result matrix. Is there a way to do this using numpy's vectorized operations without using loops?

0

1 Answer 1

2

I don't think there is a vectorized operator. The result itself is not a typical matrix since it is not rectangular. Alternatively, you can simply use list comprehension.

y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

split_at = [2, 4, 7]
output = [[y[:x], y[x:]] for x in split_at]
print(output) # [[[0, 1], [2, 3, 4, 5, 6, 7, 8, 9]], [[0, 1, 2, 3], [4, 5, 6, 7, 8, 9]], [[0, 1, 2, 3, 4, 5, 6], [7, 8, 9]]]
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.