2

I have numpy arrays of start indices and end indices from which I'd like to construct a flattened array of ranges.

e.g. with inputs

s = np.array([1,2,3])
e = np.array([4,5,10])

and output

array([1,2,3,2,3,4,3,4,5,6,7,8,9])

any way to do this efficiently?

1 Answer 1

1

How about just

np.concatenate([np.arange(x, y) for x, y in zip(s, e)])
Sign up to request clarification or add additional context in comments.

1 Comment

@Chris - While @tkerwin's answer is perfectly suited to your case, you might also want to have a look at np.r_. It's a convenient way to manually build up a sequence like this. np.r_[1:4, 2:5, 3:10] will create the same sequence. It's probably not what you want to use in this case, but is very convenient in some cases, and it's good to know about.

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.