我有一个数据列表,我正在从中创建 dict
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']
我尝试使用此代码
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]]
实际上我正在尝试以这种格式打印:
{'t1': {'h1': {'d1': {'dom1': ['a', 'b']}, 'd2': {'dom2': ['a', 'b']}}},
't2': {'h2': {'d3': {'dom3': ['a', 'b']}}}}
但我得到的输出是:
{'t2': {'h2': {'d3': {'dom3': ['a','b']}}}, 't1': {'h1': {'d2': {'dom2': ['a','b']}}}}
但在改变最后一件事后,'t1' 'd1' 值丢失