Last active
December 19, 2015 05:49
-
-
Save linnchord/5906801 to your computer and use it in GitHub Desktop.
Get the deep layer value of dict.
This file contains 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 get_dict_val(keys, dic, spliter='.'): | |
""" | |
get deep value of dict | |
etc: | |
get_dict_val('a.b.c', {'a':{'b':{'c':123}}}) # 123 | |
@keys: dict key list | |
@dic: dict object | |
@spliter: keys spliter default is '.' | |
""" | |
if spliter in keys: | |
tmp = keys.split(spliter) | |
cur = dic.get(tmp[0]) | |
if isinstance(cur, dict): | |
return get_dict_val(spliter.join(tmp[1:]), cur, spliter) | |
else: | |
return None | |
else: | |
return dic.get(keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment