使用Python在嵌套for循环中将列表添加到字典

使用Python在嵌套for循环中将列表添加到字典,python,json,boto3,Python,Json,Boto3,我现在有密码 for security_group in response["SecurityGroups"]: thing1 = security_group["something"] thing2= security_group["something2"] SG_list.append({ "thing1": thing1, "thing2": "thing2", })

我现在有密码

 for security_group in response["SecurityGroups"]:
        thing1 = security_group["something"]
        thing2= security_group["something2"]
        SG_list.append({
            "thing1": thing1,
            "thing2": "thing2",

        })
        #I want to add the below data that I get to the above dictionary "SG_list" but put the dictionaries in as a list of dictionaries into "SG_list"
        #accessing a nested element in the data structure
        for dictlist in security_group["IpPermissions"]:
            otherthing = dictlist["IpProtocol"]

            #another nested element inside the above nested element    
            for cidrIp in dictlist["IpRanges"]:
                source = cidrIp["CidrIp"]
                #append all the nested data to a dictionary
                SG_list.append({
                "Type": "Missing?",
                "Protocol": ipProtocol,
                "Port Range": portRange,
                "Source":source
                })
return SG_list
代码运行并输出一个大的字典列表。但是,我希望将两个嵌套for循环中的数据附加到从中获取的每个数据页

针对响应中的安全组[“安全组]:

例如,我希望得到如下输出:

{
    "Account ID": "123456789",
    "Region": "TODO",
    "SGName": "name",
    "SGId": "sg-1234567",
    "VpcId": "vpc-1234567",
    "List of more data":["thing1":data,"thing2":data]
  }
但是我的数据却像一本新字典一样出现了

{
    "Account ID": "123456789",
    "Region": "TODO",
    "SGName": "launch-wizard-27",
    "SGId": "sg-123467",
    "VpcId": "vpc-1234567"
  },
 {
  "thing1": data,
  "thing2": data
 }
所以在我的输出中使用

                    SG_list[0].update({
                    "Type": "Missing?",
                    "Protocol": ipProtocol,
                    "Port Range": portRange,
                    "Source":source
                    })
我有一本大字典,里面有所有的数据,比如

[
  {
    "Account ID": "123456789",
    "Region": "TODO",
    "SGName": "SGname",
    "SGId": "sg-1234567",
    "VpcId": "vpc-1234567",
    "Protocol": "tcp",
    "Port Range": 1234
  },
不管我想要什么

[
  {
    "Account ID": "123456789",
    "Region": "TODO",
    "SGName": "SGname",
    "SGId": "sg-1234567",
    "VpcId": "vpc-1234567",
    ["Type": "Missing?","Protocol": "tcp","Port Range": 1234]
  },

因此,您希望循环将字典相互添加,而不是获取字典列表,对吗

在这种情况下,您只需
使用新词典而不是追加来更新已存在的原始词典

因此,如果列表中的原始词典位于索引0中,请执行以下操作

sgu列表[0]。更新({'New':'dic'})

更清楚地说。把这个放在最后一个循环中,你原来的循环将被更新

            SG_list[0].update({
            "Type": "Missing?",
            "Protocol": ipProtocol,
            "Port Range": portRange,
            "Source":source
            })

您的格式:
[“thing1”:数据,“thing2”:数据]
无效,您不能在这样的列表中包含键/值对,只能作为
[“thing1”,数据,“thing2”,数据]
。。你能做的最好的事情就是
[{“thing1”:data},{“thing2”:data}]
,也就是说,有一个字典列表。好的,这是有意义的,那么我该如何代替
SG_list.append({“Type”:“Missing?”,“Protocol”:ipProtocol,“Port Range”:portRange,“Source”:Source})
在代码末尾,将该数据保存为自己的列表,然后将该列表附加到“SG_列表”中?谢谢!这更接近我想要的是,我现在在一个字典里有所有的信息,但是有可能得到我们更新的数据到字典里的列表中吗?你能添加一个例子吗?我真的没有得到你想要做的我在原始问题中添加了更多的内容,因为评论字符有限不,这是不可能的。但是,您可以创建一个新的dic键,并将新字典添加为元组形式的值。
SG_list[0]。更新({'NewDic':{“Type”:“Missing?”,“Protocol”:ipProtocol,“Port Range”:portRange,“Source”:Source}.items())
这是它的代码。如果这回答了你的问题,最好将其标记为已回答。