Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8194885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:57:36+00:00 2026-06-07T04:57:36+00:00

Similar question has been asked for a sorted list here , but the solution

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:57:36+00:00Added an answer on June 7, 2026 at 4:57 am

    According to this great answer from @gnibbler, you can rewrite bisect code yourself to fit your need

    I 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

    def reverse_binary_search(a, x, lo=0, hi=None):
        if lo < 0:
            raise ValueError('lo must be non-negative')
        if hi is None:
            hi = len(a)
        while lo < hi: 
            mid = (lo+hi)/2
            if x > a[mid][4]:
                hi = mid 
            else:
                lo = mid+1
        return lo
    
    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]]
    threshold = [0.97, 0.90, 0.83, 0.6]
    
    index_list = []
    last_index = 0
    for t in threshold:
        last_index = reverse_binary_search(my_list, t, last_index) # next time start search from last_index
        index_list.append(last_index)
    

    Thanks @PhilCooper for valuable suggestions. Here is the code using generator as he proposed:

    def reverse_binary_search(a, threshold):
        lo = 0
        for t in threshold:
            if lo < 0:
                raise ValueError('lo must be non-negative')
            hi = len(a)
            while lo < hi: 
                mid = (lo+hi)/2
                if t > a[mid][6]:
                    hi = mid 
                else:
                    lo = mid+1
            yield lo
    
    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]]
    threshold = [0.97, 0.90, 0.83, 0.6]
    
    index_list = list(reverse_binary_search(my_list, threshold))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know a similar question has been asked but I have not found a
I'm afraid very similar question has been asked already here , but for some
I am not sure if a similar question has been asked before, searched for
First of all I know similar questions has been asked here, I've checked but
A similar question has been asked, but since it always depends, I'm asking for
A similar question has been asked before by someone else , but there were
a similar question has been asked before , but the answers suggested a workaround
I'm sorry, I'm sure a similar question has already been asked but I'm afraid
I know similar kind of question has been asked many times but seriously i
I know a similar question on this topic has been asked, but doesn't look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.