I have a list of data from which I am creating dict inside dict, structure it is coming as expected but somewhere it is overwriting, I don’t know where
a=['t1_h1','t2_h2']
b=['h1_d1','h1_d2','h2_d3']
c=['d1_dom1','d2_dom2','d3_dom3']
d=['dom1_a','dom1_b','dom2_a','dom2_b','dom3_a','dom3_b']
I tried using this code
for item in a:
f[item.split('_')[0]]={}
for hypercube in b:
if item.split('_')[1] in hypercube:#h1 in b
f[item.split('_')[0]][item.split('_')[1]]={}
for dimension in c:
if hypercube.split('_')[1] in dimension:#d1 in c
f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]]={}
for domain in d:
if dimension.split('_')[1] in domain:#dom1 in d
if f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]].has_key(dimension.split('_')[1]):
f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]][dimension.split('_')[1]].append(domain.split('_')[1])
else:
f[item.split('_')[0]][item.split('_')[1]][hypercube.split('_')[1]][dimension.split('_')[1]]=[domain.split('_')[1]]
Actually I am trying to print in this format:
{'t1': {'h1': {'d1': {'dom1': ['a', 'b']}, 'd2': {'dom2': ['a', 'b']}}},
't2': {'h2': {'d3': {'dom3': ['a', 'b']}}}}
But the output I am getting is:
{'t2': {'h2': {'d3': {'dom3': ['a','b']}}}, 't1': {'h1': {'d2': {'dom2': ['a','b']}}}}
but after changing last thing is in ‘t1’ ‘d1’ value is missing
You don’t want to nest your for loops, you want to nest the for searches as you process each list a, b, c and d, adding deeper and deeper levels to f. This gives your desired output:
prints: