Given a sorted list
and a number n
, find the index in the list that precedes n
in the most efficient (fastest) way.
sorted list example:
x_list = [1, 3.5, 5, 9.2, 20, 50.75]
number n
, say n = 7.5
Example answer: the index of the value in the list that precedes n
is 2
.
This is what i have tried so far:
x_list = [1, 3.5, 5, 9.2, 20, 50.75]
n = 7.5
for i, v in enumerate(x_list):
if v < n: xlow = i
else: break
print(xlow)
Can i do a quicker find than the above method ?
Note:
This list is sorted
whereas other questions of the same nature are unsorted
.