我有一个dict dict的词典,我希望通过组合两个最外层的键来转换为dict的dict。是否有一种优雅的方式来做到这一点?
这是最初的词典:
alldic={}
for a in range(3):
machinedic={}
for x in range(5):
machinetimedic={}
for i in range(4):
machinetimedic["variable0"]=0
machinetimedic["variable1"]=1
machinedic["time "+str(x)]=machinetimedic
alldic["machine "+str(a)]=machinedic
我想要的是dict的词典,就像那样:
{'machine 0-time 0': {'variable0': 0, 'variable1': 1},
'machine 0-time 1': {'variable0': 0, 'variable1': 1},
'machine 0-time 2': {'variable0': 0, 'variable1': 1},
'machine 0-time 3': {'variable0': 0, 'variable1': 1},
'machine 0-time 4': {'variable0': 0, 'variable1': 1},
'machine 1-time 0': {'variable0': 0, 'variable1': 1},
'machine 1-time 1': {'variable0': 0, 'variable1': 1},
'machine 1-time 2': {'variable0': 0, 'variable1': 1},
'machine 1-time 3': {'variable0': 0, 'variable1': 1},
'machine 1-time 4': {'variable0': 0, 'variable1': 1},
'machine 2-time 0': {'variable0': 0, 'variable1': 1},
'machine 2-time 1': {'variable0': 0, 'variable1': 1},
'machine 2-time 2': {'variable0': 0, 'variable1': 1},
'machine 2-time 3': {'variable0': 0, 'variable1': 1},
'machine 2-time 4': {'variable0': 0, 'variable1': 1}}
答案 0 :(得分:1)
不是嵌套,而是像这样展平它
alldic={}
for a in range(3):
machinedic={}
for x in range(5):
machinetimedic={}
for i in range(4):
machinetimedic["variable0"]=0
machinetimedic["variable1"]=1
alldic["machine {}-time {}".format(a, x)] = machinetimedic
或者,您可以使用字典理解获得相同的结果,例如
>>> {"machine {}-time {}".format(a, x) : {"variable0": 0, "variable1" : 1}
for a in range(3) for x in range(5)}
{'machine 0-time 0': {'variable0': 0, 'variable1': 1},
'machine 0-time 1': {'variable0': 0, 'variable1': 1},
'machine 0-time 2': {'variable0': 0, 'variable1': 1},
'machine 0-time 3': {'variable0': 0, 'variable1': 1},
'machine 0-time 4': {'variable0': 0, 'variable1': 1},
'machine 1-time 0': {'variable0': 0, 'variable1': 1},
'machine 1-time 1': {'variable0': 0, 'variable1': 1},
'machine 1-time 2': {'variable0': 0, 'variable1': 1},
'machine 1-time 3': {'variable0': 0, 'variable1': 1},
'machine 1-time 4': {'variable0': 0, 'variable1': 1},
'machine 2-time 0': {'variable0': 0, 'variable1': 1},
'machine 2-time 1': {'variable0': 0, 'variable1': 1},
'machine 2-time 2': {'variable0': 0, 'variable1': 1},
'machine 2-time 3': {'variable0': 0, 'variable1': 1},
'machine 2-time 4': {'variable0': 0, 'variable1': 1}}
答案 1 :(得分:1)
迭代效果很好:
>>> all_new_dic = {}
>>> for key in alldic:
for k in alldic[key]:
new_key = key + '-' + k
all_new_dic[new_key] = alldic[key][k]
>>> all_new_dic
{'machine 1-time 4': {'variable1': 1, 'variable0': 0}, 'machine 1-time 0': {'variable1': 1, 'variable0': 0}, 'machine 1-time 1': {'variable1': 1, 'variable0': 0}, 'machine 1-time 2': {'variable1': 1, 'variable0': 0}, 'machine 1-time 3': {'variable1': 1, 'variable0': 0}, 'machine 2-time 4': {'variable1': 1, 'variable0': 0}, 'machine 2-time 1': {'variable1': 1, 'variable0': 0}, 'machine 2-time 0': {'variable1': 1, 'variable0': 0}, 'machine 2-time 3': {'variable1': 1, 'variable0': 0}, 'machine 2-time 2': {'variable1': 1, 'variable0': 0}, 'machine 0-time 3': {'variable1': 1, 'variable0': 0}, 'machine 0-time 2': {'variable1': 1, 'variable0': 0}, 'machine 0-time 1': {'variable1': 1, 'variable0': 0}, 'machine 0-time 0': {'variable1': 1, 'variable0': 0}, 'machine 0-time 4': {'variable1': 1, 'variable0': 0}}