Similar question has been asked for a sorted list here, but the solution used bisect which is not working for reserve sorted list.
Say I have a list, sorted in reverse order, keyed on the middle element,
my_list = [[3,0.99,1], [2,0.98,54], [10,.85,4], [1,0.7,10], [12,0.69,31], [12,0.65,43], [1.56,0] ....]
and I want to apply a series of threshold values on the middle element, which is in a separate sorted list, say
threshold = [0.97, 0.90, 0.83, 0.6]
I am trying to find out the index of the first element smaller than the threshold value. In the above example it should return,
index_list = [2, 2, 3, 6]
Suggestiong on how can it be done in the fastest way ?
According to this great answer from @gnibbler, you can rewrite
bisectcode yourself to fit your needI modify the code from @gnibbler slightly, so that it can be used in your case
An optimization is that since your thresholds are also sorted, we don’t need to search the whole list each time, but start from the last result index
Thanks @PhilCooper for valuable suggestions. Here is the code using generator as he proposed: