Created
September 12, 2017 09:57
-
-
Save sidazhou/5f1fb6f9489486b05ae783a535f729b1 to your computer and use it in GitHub Desktop.
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
''' | |
Get a overview of the structure of a variable that is nested dict of list of dict ... | |
Usage: | |
recur_get_keys_print(dictlist) | |
''' | |
def recur_get_keys(dict_or_list, count=1): | |
if type(dict_or_list) is dict: | |
for key, value in dict_or_list.items(): | |
yield { | |
'depth': count, | |
'type': 'dict', | |
'key': key, | |
'item_count': 0, | |
} | |
if type(value) is dict or type(value) is list: | |
for dic in recur_get_keys(value, count+1): | |
yield dic | |
elif type(dict_or_list) is list: | |
yield { | |
'depth': count, | |
'type': 'list', | |
'key': 0, | |
'item_count': len(dict_or_list), | |
} | |
value = dict_or_list[0] # assuming all list elem are identical type | |
if type(value) is dict or type(value) is list: | |
for dic in recur_get_keys(value, count+1): | |
yield dic | |
def recur_get_keys_print(dictlist): | |
for item in recur_get_keys(dictlist): # "visualizing" | |
if item['depth'] == 1: # for clarity | |
print '' | |
print ' '*item['depth'], | |
if item['type'] == 'dict': | |
print '{', | |
print item['key'], | |
elif item['type'] == 'list': | |
print '[', | |
print item['item_count'], | |
print '' | |
l = { | |
'k1': 3, | |
'k2': {'kk1': 4}, | |
'k3': [1,2,3], | |
'k4': [[1,2],3], | |
'k5': {'kk5':[{'kkk1': 666},{'kkk2': 666}] }, | |
'k6': [[1,[{'kkk': 666},3]],3], | |
'g': {'c': [[3,2],[1,1]], 'o1': 666}, | |
'g2': {'o1': 666, 'c': [1,1]}, | |
} | |
recur_get_keys_print(l) | |
# output: | |
# { k3 | |
# [ 3 | |
# | |
# { k2 | |
# { kk1 | |
# | |
# { k1 | |
# | |
# { g | |
# { c | |
# [ 2 | |
# [ 2 | |
# { o1 | |
# | |
# { k6 | |
# [ 2 | |
# [ 2 | |
# | |
# { k5 | |
# { kk5 | |
# [ 2 | |
# { kkk1 | |
# | |
# { k4 | |
# [ 2 | |
# [ 2 | |
# | |
# { g2 | |
# { c | |
# [ 2 | |
# { o1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment