I'm trying to search through a big python dictionary for items that have these parameters ->
"df" between 3 and 4
"ar" between 6 and 9
"od" between 1 and 2
The json string looks something like this
values = { "key1" : { "ar" : 8, "df" : 3, "od" : 1, "id" : 1} ,
"key2" : { "ar" : 5, "df" : 3, "od" : 5, "id" : 2},
"key3" : { "ar" : 6, "df" : 4, "od" : 2, "id" : 3}}
I want to get only the values from the ones that meet my condition (key1 and 3), but I also don't want to make a whole lines of codes for each combination possible (in case I just want to search for "od" and "df" or only "ar"). I managed to search through only value as the example below
ar_range_min = 6
ar_range_max = 9
df_range_min = 3
df_range_min = 4
myDict = { "key1" : { "ar" : 8, "df" : 3, "od" : 1, "id" : 1} ,
"key2" : { "ar" : 5, "df" : 3, "od" : 5, "id" : 2},
"key3" : { "ar" : 6, "df" : 4, "od" : 2, "id" : 3}}
def df_search(df_min, df_max):
global values, df_range
listOfDicts = []
foreach k in myDict:
z = z+1
if df_min <= k["df"] >= df_max:
listOfDicts.append(k)
return listOfDicts
def ar_search(ar_min, ar_max):
global values
listOfDicts = []
foreach k in myDict:
z = z+1
if ar_min <= k["ar"] >= ar_max:
listOfDicts.append(k)
return listOfDicts
match_df = df_search(df_range_min, df_range_max)
match_ar = ar_search(ar_range_min, ar_range_max)
match_both = ???
What I don't know is how to create a new dictionary with the values in common between match_df and match_ar and how to merge them if I have 3 dictionaries (parameters)
PS: This I not the actual dict, it has more nested keys in each Children