user9061464
user9061464

Reputation:

How to split an array in unequal pieces?

I have an array like this

import numpy as np

x = np.array([1, 2, 3, 99, 99, 3, 2, 1])

I want to split it in three pieces

x1 = array([1, 2, 3])
x2 = array([99, 99])
x3 = array([3, 2, 1])

What is the best way to do this?

Upvotes: 0

Views: 1534

Answers (1)

Cleb
Cleb

Reputation: 25997

You can use np.split:

x = np.array([1, 2, 3, 99, 99, 3, 2, 1])

x1, x2, x3 = np.split(x, [3, 5])

[3, 5] thereby specifies the indexes at which you want to split.

That yields

x1
array([1, 2, 3])

x2
array([99, 99])

x3
array([3, 2, 1])

Upvotes: 6

Related Questions