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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:35:57+00:00 2026-06-06T17:35:57+00:00

I have a dictionary structure that maps an id (integer) into a number (double).

  • 0

I have a dictionary structure that maps an id (integer) into a number (double). The numbers are actually weights of an item.

I am writing a function that will allows me to fetch the id of a given weight (if the weight is found in the dict, else, it will return the id of the next closest (i.e. nearest matching) weight.

This is what I have so far:

def getBucketIdByValue(bucketed_items_dict, value):
    sorted_keys = sorted(bucketed_items_dict.keys())
    threshold = abs(bucketed_items_dict[sorted_keys[-2]] -bucketed_items_dict[sorted_keys[-1]]) # determine gap size between numbers

    # create a small dict containing likely candidates
    temp = dict([(x - value),x] for x in bucketed_items_dict.values() if abs(x - value) <= threshold)
    print 'DEBUG: Deviations list: ', temp.keys()
    smallest_deviation = min(temp.keys()) if value >= 0 else max(temp.keys()) # Not sure about this ?
    smallest_deviation_key = temp[smallest_deviation]
    print 'DEBUG: found bucketed item key:',smallest_deviation_key
    return smallest_deviation_key

I’m not sure the logic is actually correct (esp. where I obtain the smallest deviatioon). In any event, if even the logic is correct, this seems an overly complicated way of doing things. Is there a more elegant/pythonic way of doing this?

Off the top of my head, I think a more pythonic/elegant way would be to do something like passing a custom function to the min function – don’t know if that is possible…

[[Update]]

I am running Python 2.6.5

  • 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-06T17:36:00+00:00Added an answer on June 6, 2026 at 5:36 pm

    Try sorting the items by the distance of their weight to your target value:

    from operator import itemgetter
    distances = ((k, abs(v - value)) for k, v in bucketed_items_dict.items())
    return min(distances, key=itemgetter(1))[0]
    

    Or using a lambda function instead of itemgetter:

    distances = ((k, abs(v - value)) for k, v in bucketed_items_dict.items())
    return min(distances, key=lambda x:x[1])[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary data structure that must be passed around using WCF. To
I have a data structure that is basically a document with a dictionary of
Does VBA have dictionary structure? Like key<>value array?
I have a dictionary of unknown depth and structure. It might contain more dictionaries,
I have the following url structure in rails routes: match '/financial-dictionary/:letter' => 'learning_center#dictionary', :as
Have Dictionary <Int64, byte> that gets used a lot. I mean in a loop
I have a dictionary with the following structure: {'ONE' : (4, 6, 9), 'TWO'
I have a dictionary of dictionaries with the following structure: 1:{'Amendment': '1', 'status': 'Stadf\xc3\xa6stet',
I have two dictionaries with the same structure: Dictionary<string, int> foo = new Dictionary<string,
I have a Dictionary<..., Statistics> , where Statistics is a custom structure with some

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.