if I have a dictionary like this
>>> d = {10: 3, 100: 2, 1000: 1}
I can type something like:
>>> d.get(10), d
bisect module allows fast lookup of insertion position in a sorted list.
from bisect import bisect_right
def closest_matches(data, query):
keys = sorted(data)
return [data[i] for i in (min(map(abs, (keys[p-1], keys[p]))) for p in (bisect_right(keys, k) for k in query))]
>>> d = {10: 3, 100: 2, 1000: 1}
>>> closest_matches(d, [20, 60, 200])
[3, 3, 2]