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.
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:
UPDATE: Code improved as suggested by Mark Dickinson. Thanks!