Last active
December 29, 2016 21:51
-
-
Save bpeterso2000/56cee62b78454c0d7142314fc3619e66 to your computer and use it in GitHub Desktop.
Get pointers to all unique top-level and nested keys
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
import collections | |
def get_keys(d, keys=None, pointer=None, sep='.', prefix=''): | |
if keys is None: | |
keys = set() | |
if pointer is None: | |
pointer = [] | |
for key, value in d.items(): | |
keyptr = pointer + [str(key)] | |
keys.add(prefix + sep.join(keyptr)) | |
if isinstance(value, collections.Mapping): | |
keys |= get_keys(value, None, keyptr, sep, prefix) | |
return keys | |
keys = (get_keys(i) for i in items) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment