Skip to content Skip to sidebar Skip to footer

Numpy Version Of This Particular List Comprehension

So a few days ago I needed a particular list comprehension in this thread: Selecting a subset of integers given two lists of end points and I got a satisfying answer. Fast forward

Solution 1:

If it is still of interest to you, you could get rid of one for loop and use numpy.arange() in combination with list comprehension and numpy.hstack() to get what is desired. Having said that we would still need at least one for loop to get this done (because neither range nor arange accept a sequence of endpoints)

t1 = [0,13,22]
t2 = [4,14,25]
np.hstack([np.arange(r[0], r[1]+1)  for r in zip(t1, t2)])

# outputs
array([ 0,  1,  2,  3,  4, 13, 14, 22, 23, 24, 25])

However, I don't know how much more performant this is going to be for your specific case.

Post a Comment for "Numpy Version Of This Particular List Comprehension"