I have a dict data structure with various "depths". By "depths" I mean for example: When depth is 1, dict will be like:
{'str_key1':int_value1, 'str_key2:int_value2}
When depth is 2, dict will be like:
{'str_key1':
{'str_key1_1':int_value1_1,
'str_key1_2':int_value1_2},
'str_key2':
{'str_key2_1':int_value2_1,
'str_key2_2':int_value2_2} }
so on and so forth.
When I need to process the data, now I'm doing this:
def process(keys,value):
#do sth with keys and value
pass
def iterate(depth,dict_data):
if depth == 1:
for k,v in dict_data:
process([k],v)
if depth == 2:
for k,v in dict_data:
for kk,vv, in v:
process([k,kk],v)
if depth == 3:
.........
So I need n for loops when depth is n. As depth can go up to 10, I'm wondering if there is a more dynamic way to do the iteration without having to write out all the if and for clauses.
Thanks.