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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:43:40+00:00 2026-06-01T20:43:40+00:00

I have a dict with floats as keys and objects as values. I receive

  • 0

I have a dict with floats as keys and objects as values.
I receive a float, and I’d like to know between what two keys this float is.
How do I find this?

Example of what I mean in code:

a = {}
a[1.2] = some_unimportant_instance
a[2.3] = some_other_unimportant_instance
a[2.6] = some_third_unimportant_instance
etc...

r = 2.5
# a[r] will not work
# I want something that returns the two numbers around r
# in this case, 2.3 and 2.6.
  • 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-01T20:43:42+00:00Added an answer on June 1, 2026 at 8:43 pm

    First observation: dict-s are bad for this. They are implemented using hashes and are efficient for retrieving values only for exact matches. For your purpose, you would have to first transform the dict into a list of keys. Then you could use modules such as bisect.

    Example:

    import bisect
    keys = sorted(a.keys())
    index = bisect.bisect(keys, r)
    if index >= 1:
        print keys[index - 1]
    print keys[index]
    

    UPDATE: Code improved as suggested by Mark Dickinson. Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dict that has string-type keys whose exact values I can't know
I have a rowset that looks like this: defaultdict(<type 'dict'>, { u'row1': {u'column1': 33,
I know that I can hash singular values as keys in a dict .
I have a dict that looks like this { keyword1:3 , keyword2:1 , keyword3:5
I have a dict like this: food_to_food_type = {apple: fruit, green bean: vegetable, tomato:
I have a dict, that looks like this: { 'foo': { 'opt1': 1, 'opt2':
I have a dictionary of string keys and float values. mydict = {} mydict[joe]
I have a dict like this: {1: ['1,2,3', '4,5,6', '7,8']} the dict can be
I have a dict like this: d = {'a':'b+c', 'b':'f+g', 'f':'y+u'} I want to
Have dict like: mydict= {'a':[],'b':[],'c':[],'d':[]} list like: log = [['a',917],['b', 312],['c',303],['d',212],['a',215],['b',212].['c',213],['d',202]] How do i

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.