4

I have an array:

a = [1, 3, 5, 7, 29 ... 5030, 6000]

This array gets created from a previous process, and the length of the array could be different (it is depending on user input).

I also have an array:

b = [3, 15, 67, 78, 138]

(Which could also be completely different)

I want to use the array b to slice the array a into multiple arrays.

More specifically, I want the result arrays to be:

array1 = a[:3]
array2 = a[3:15]
...
arrayn = a[138:]

Where n = len(b).

My first thought was to create a 2D array slices with dimension (len(b), something). However we don't know this something beforehand so I assigned it the value len(a) as that is the maximum amount of numbers that it could contain.

I have this code:

 slices = np.zeros((len(b), len(a)))

 for i in range(1, len(b)):
     slices[i] = a[b[i-1]:b[i]]

But I get this error:

ValueError: could not broadcast input array from shape (518) into shape (2253412)
1
  • 1
    I'm surprised I couldn't find a duplicate question. Good work! Commented May 12, 2017 at 19:21

3 Answers 3

6

You can use numpy.split:

np.split(a, b)

Example:

np.split(np.arange(10), [3,5])
# [array([0, 1, 2]), array([3, 4]), array([5, 6, 7, 8, 9])]
Sign up to request clarification or add additional context in comments.

Comments

2
b.insert(0,0)
result = []
for i in range(1,len(b)):
    sub_list = a[b[i-1]:b[i]]
    result.append(sub_list)
result.append(a[b[-1]:])

1 Comment

This is essentially what np.split does, with a little fancier footwork to allow split on other axis.
2

You are getting the error because you are attempting to create a ragged array. This is not allowed in numpy.

An improvement on @Bohdan's answer:

from itertools import zip_longest
result = [a[start:end] for start, end in zip_longest(np.r_[0, b], b)]

The trick here is that zip_longest makes the final slice go from b[-1] to None, which is equivalent to a[b[-1]:], removing the need for special processing of the last element.

Please do not select this. This is just a thing I added for fun. The "correct" answer is @Psidom's answer.

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.