Last active
July 30, 2019 13:13
-
-
Save antimirov/b1ba9ae6d64aef7bcddff12423af41c5 to your computer and use it in GitHub Desktop.
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 gen_dict_extract(key, var, path=None): | |
| if path is None: | |
| path = [] | |
| if hasattr(var, "items"): | |
| for k, v in var.items(): | |
| if k == key: | |
| yield v, path + [k] | |
| if isinstance(v, dict): | |
| for result, _path in gen_dict_extract(key, v, path + [k]): | |
| yield result, _path | |
| elif isinstance(v, list): | |
| for idx, d in enumerate(v): | |
| for result, _path in gen_dict_extract(key, d, path + [f"{k}[{idx}]"]): | |
| yield result, _path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment