Created
August 17, 2018 10:59
-
-
Save gustavi/3ec55d12f470e304bfea6a7986242b6d to your computer and use it in GitHub Desktop.
explore 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
Python 2.7.13 (default, Nov 24 2017, 17:33:09) | |
[GCC 6.3.0 20170516] on linux2 | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> def foo(path, struct): | |
... value = struct | |
... for x in path.split('.'): | |
... value = value[x] | |
... return value | |
... | |
>>> d = {'a': {'b': {'c': 42}, 'd': list()}} | |
>>> foo('a.b.c', d) | |
42 | |
>>> foo('a.d', d) | |
[] | |
>>> foo('a', d) | |
{'b': {'c': 42}, 'd': []} | |
>>> |
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 foo(path, struct): | |
value = struct | |
for x in path.split('.'): | |
value = value[x] | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment