我正在尝试创建基于循环索引的字典名称,例如mydict_1、mydict_2等,或者将字典附加到一个字典中
通过一个循环,我获得了数据集,并且我希望能够一次访问所有数据集或逐个访问它们。
for components in fiSentence.findall("components"):
operation = components.find('operation').text
target = components.find('target').text
targetState = components.find('targetState').text
...所有这些都收录在字典里:
tempDict = {"operation":operation, "target":target, "targetState":targetState, ...}然后在循环之外,我试图将它们全部存储在另一个字典中,但我只能用一个列表来实现:
data.append(tempDict)我想要的是将它们存储在不同的字典中,如下所示:
procedural_Step_1 = {"operation":operation, "target":target, "targetState":targetState}
procedural_Step_2 = {"operation":operation, "target":target, "targetState":targetState}
...或者将它们全部存储在一个字典中:
data = {"procedural_Step_1":{"operation":operation, "target":target, "targetState":targetState}, {"procedural_Step_2":{"operation":operation, "target":target, "targetState":targetState},...}发布于 2019-11-07 17:55:25
您可以在循环之前和循环结束时声明dict data:
data['procedural_step_'+str(index)] = temp_dict使用enumerate可以获得的索引
https://stackoverflow.com/questions/58745970
复制相似问题