Reputation: 3473
I have a list of values that I know are increasing, such as
x = [1, 2, 3, 4, 5, 6]
I'm looking for the indices of the subset that are within some range [min, max]
. E.g. I want
>> subset_indices(x, 2, 4)
[1, 3]
>> subset_indices(x, 1.1, 7)
[1, 5]
Is there a nice pythonic way of doing this?
Upvotes: 0
Views: 124
Reputation: 3473
Following the recommendations from Kenny Ostrom and volcano, I implemented it simply as
import bisect
def subset_indices(sequence, minv, maxv):
low = bisect.bisect_left(sequence, minv)
high = bisect.bisect_left(sequence, maxv, lo=low)
return [low, high]
Upvotes: 1