【发布时间】:2020-03-03 20:54:15
【问题描述】:
我正在尝试 根据循环索引创建字典名称,例如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},...}
【问题讨论】:
标签: python dictionary variables append