Created
October 13, 2013 02:19
-
-
Save markrwilliams/6957361 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 collect_keys(d): | |
if not isinstance(d, dict): | |
return [] | |
level = d.keys() | |
for k, v in d.items(): | |
level.extend('%s.%s' % (k, c) for c in collect_keys(v)) | |
return level | |
def analyze(paths): | |
survey = {} | |
for p in paths: | |
survey[p] = survey.setdefault(p, 0) + 1 | |
return survey | |
def survey_dicts(lod): | |
return analyze([path | |
for d in lod | |
for path in collect_keys(d)]) | |
lod = [{'d': {'c': 1, 'e': 2}, 'a': 3}, | |
{'d': {'c': 1, 'f': 1}}, | |
{'a': 4}] | |
print survey_dicts(lod) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment