I am using defaultdicts to store lists of values where the keys
are periods for which values could be observed.
When looking up from a list of all periods of interest, I would like to find the closest period in my defaultdict (NB: not all periods are stored in the defaultdict).
As defaultdicts are not sorted however, the below approach does not return the correct value.
Is there a different way of returning the closest available key for defaultdicts?
from collections import defaultdict
import numpy as np
def_dict = defaultdict(list)
# entries that will be stored in the defaultdict
reg_dict = {0: ["a", "b"], 2: ["c", "d"], 5: ["k", "h"], -3: ["i", "l"]}
# store items from regular dict in defaultdict
for k, v in reg_dict.items():
def_dict[k] = v
# Lookup periods
periods = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8]
for period in periods:
# this approach does not return the right keys as defaultdicts are not sorted
closest_key = np.abs(np.array(list(def_dict.keys())) - period).argmin()
print("period: ", period, " - looked up key: ", closest_key)
This returns the following:
period: -1 - looked up key: 0
period: 0 - looked up key: 0
period: 1 - looked up key: 0
period: 2 - looked up key: 1
period: 3 - looked up key: 1
period: 4 - looked up key: 2
period: 5 - looked up key: 2
period: 6 - looked up key: 2
period: 7 - looked up key: 2
period: 8 - looked up key: 2
The way I understand, you want an output similar to this?
[0, 0, 0, 2, 2, 5, 5, 5, 5, 5]
For the above, the logic would be
closest_key = [min(def_dict.keys(), key = lambda x: abs(x - p)) for p in periods]
Specifying the optional key
parameter to built in python functions is useful in situations like these.