Skip to content

Instantly share code, notes, and snippets.

@linnchord
Last active December 19, 2015 05:49
Show Gist options
  • Save linnchord/5906801 to your computer and use it in GitHub Desktop.
Save linnchord/5906801 to your computer and use it in GitHub Desktop.
Get the deep layer value of dict.
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