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 6636207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:09:25+00:00 2026-05-25T23:09:25+00:00

I have a list of many Python objects like this: class RangeClass(object): def __init__(self,address,size):

  • 0

I have a list of many Python objects like this:

class RangeClass(object):

    def __init__(self,address,size):
        self.address=address
        self.size=size
        #other attributes and methods...

Then, I have a list (rangelist) of RangeClass objects.

I need to find within which range a given value is.

I can use some code like this:

for r in ragelist:
    if(value>=r.address and value<(r.address+r.size)):
        return r
return None

But I think there is a faster way. Ranges have arbitrary size, but we can assume that they don’t overlap.

Thank you.

  • 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-05-25T23:09:26+00:00Added an answer on May 25, 2026 at 11:09 pm

    If you have many values to test, then you could use the bisect module to find which range the values are in more quickly.

    If

    • m = the number of values to test, and
    • n = len(rangelist)

    then looping through the values and rangelist as you suggest would take O(m*n) time.

    If you use bisection, then you must first sort the starting addresses O(nlogn) and find each value’s place in rangelist O(m*logn).
    So if

    O(nlogn + m*logn) < O(m*n)
    

    then bisection wins. For large n, O(m*logn) is miniscule compared to O(m*n).
    So the inequality above would be true if

    O(nlogn) < O(m*n)
    

    or equivalently, when

    C log(n) < m
    

    for some constant C.


    Thus, when n is large and C log(n) < m, you might try something like

    import bisect
    
    class RangeClass(object):
    
        def __init__(self,address,size):
            self.address=address
            self.size=size
        def __str__(self):
            return '[{0},{1})'.format(self.address,self.address+self.size)
        def __lt__(self,other):
            return self.address<other.address
    
    rangelist=sorted([RangeClass(i,1) for i in (1,3,4,5,7.5)])
    starts=[r.address for r in rangelist]
    
    def find_range(value):
        start_idx=bisect.bisect(starts,value)-1
        try:
            r=rangelist[start_idx]
        except IndexError:
            return None
        start=r.address
        end=r.address+r.size
        if start<=value<end:
            return rangelist[start_idx]
        return None    
    
    print(','.join(str(r) for r in rangelist))
    
    for value in (0,1,1.5,2,3,4,5,6,7,8,9,10):
        r=find_range(value)
        if r:
            print('{v} in {r}'.format(v=value,r=str(r)))
        else:
            print('{v} not in any range'.format(v=value))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have List objects which are shown like this: www.mysite.com/lists/123 Where 123 is the
Does anyone know how many options a drop down list can have? Is it
I have many items inside a list control. I want each item to have
I have a class model designed for a person class in python. where a
I have something like the follwing. A person having many colors of cars of
So I have this code in python that writes some values to a Dictionary
I have many variable-sized lists containing instances of the same class with attribute foo,
We have a database library in C# that we can use like this: DatabaseConnection
I'm looking for a perfect text editor :) The must have list: vim-like modal
I have a Python program that spawns many threads, runs 4 at a time,

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.