Created
September 10, 2013 19:43
-
-
Save goldhand/6514540 to your computer and use it in GitHub Desktop.
Pass a list of dict conditions to find a particular
dict in a recursive set of dicts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_conditions(conditions={}, D={}): | |
for key in conditions: | |
if D[key] == conditions[key]: | |
continue | |
else: | |
return False | |
return True | |
def get_dict_from_list(DL=[], conditions=[], i=0, children_key='children'): | |
""" | |
looks through a list of dictionaries and returns dict, where dict[key] == value | |
:param args: | |
:param DL: | |
:param i: | |
""" | |
for D in DL: | |
if check_conditions(conditions=conditions[i], D=D): | |
i += 1 | |
if len(conditions) > i: | |
if D[children_key]: | |
get_dict_from_list(DL=D[children_key], conditions=conditions, i=i, children_key=children_key) | |
else: | |
i -= 1 | |
pass | |
else: | |
return D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Keep getting this error: