命名变量或追加字典

我试图 根据循环索引创建字典名称,例如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,"targetState":targetState}
procedural_Step_2 = {"operation":operation,"targetState":targetState}
...

或 将它们全部存储在一本字典中:

data = {"procedural_Step_1":{"operation":operation,"targetState":targetState},{"procedural_Step_2":{"operation":operation,...}
yanhua0801 回答:命名变量或追加字典

您可以在循环之前和循环结束时声明字典data

   data['procedural_step_'+str(index)] = temp_dict

您可以通过enumerate获得的索引

本文链接:https://www.f2er.com/3146005.html

大家都在问