Reputation: 24084
I have two arrays that are used as indices
a = np.array([0, 5, 11])
b = np.array([2, 8, 13])
How can I select the range between these arrays? i.e. 0 - 2, 5 - 8, 11 - 13
c = np.array([0,1,2,5,6,7,8,11,12,13])
so that I can use
data[c] # selects all the elements between ranges a and b
How do I construct array c
? I'm looking for a numpy solution
Upvotes: 1
Views: 464
Reputation: 500177
>>> x = np.arange(20)
>>> a = [0, 5, 11]
>>> b = [2, 8, 13]
>>> np.hstack(x[start:stop+1] for start, stop in zip(a, b))
array([ 0, 1, 2, 5, 6, 7, 8, 11, 12, 13])
Upvotes: 4
Reputation: 361565
>>> zip(a, b)
[(0, 2), (5, 8), (11, 13)]
>>> [range(l,h+1) for l,h in zip(a, b)]
[[0, 1, 2], [5, 6, 7, 8], [11, 12, 13]]
>>> list(itertools.chain.from_iterable(range(l,h+1) for l,h in zip(a, b)))
[0, 1, 2, 5, 6, 7, 8, 11, 12, 13]
Upvotes: 3