简体   繁体   English

python如何组合两个列表(键/值对),其中包含字典列表

[英]python how to combine two list (key/vaule pairs), which has list of dicts

I have two lists and each list has multiple items.我有两个列表,每个列表都有多个项目。 here is the example:这是示例:

x: X:

[
    {
        "uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "name": "general_c",
        "generation": 0,
        "root_provider_uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "parent_provider_uuid": null
    },
    {
        "uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "name": "aggr",
        "generation": 1,
        "root_provider_uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "parent_provider_uuid": null
    },
    {
        "uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "name": "c001",
        "generation": 961,
        "root_provider_uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "parent_provider_uuid": null
    }
]

lst:第一:

[
    [],
    [
        {
            "resource_class": "VCPU",
            "allocation_ratio": 16.0,
            "min_unit": 1,
            "max_unit": 64,

        },
        {
            "resource_class": "MEMORY_MB",
            "allocation_ratio": 1.5,
            "min_unit": 1,
            "max_unit": 257653,

        }
    ],
    [
        {
            "resource_class": "VCPU",
            "allocation_ratio": 4.0,
            "min_unit": 1,
            "max_unit": 64,

        },
        {
            "resource_class": "MEMORY_MB",
            "allocation_ratio": 2.0,
            "min_unit": 1,
            "max_unit": 257589,

        }
    ]
]

desired output:所需的输出:

new_list:新列表:

[
    {
        "uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "name": "general_c",
        "generation": 0,
        "root_provider_uuid": "b9e9d655-64a8-4d74-a9bc-3967347f7928",
        "parent_provider_uuid": null
        "resource_allocation: []"
    },
    {
        "uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "name": "aggr",
        "generation": 1,
        "root_provider_uuid": "94d3d093-3608-4cf1-982b-3897ed77d59a",
        "parent_provider_uuid": null
        "resource_allocation: 
                [
                    {
                        "resource_class": "VCPU",
                        "allocation_ratio": 16.0,
                        "min_unit": 1,
                        "max_unit": 64,
            
                    },
                    {
                        "resource_class": "MEMORY_MB",
                        "allocation_ratio": 1.5,
                        "min_unit": 1,
                        "max_unit": 257653,
            
                    }
                ]
    },
    {
        "uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "name": "c001",
        "generation": 961,
        "root_provider_uuid": "59758b2f-8faa-4eae-b838-a31fcc40c2c2",
        "parent_provider_uuid": null
        "resource_allocation: 
                [
                      {
                          "resource_class": "VCPU",
                          "allocation_ratio": 4.0,
                          "min_unit": 1,
                          "max_unit": 64,
                
                      },
                      {
                          "resource_class": "MEMORY_MB",
                          "allocation_ratio": 2.0,
                          "min_unit": 1,
                          "max_unit": 257589,
                
                      }
                  ]
    }
]

i want to create a new key called "resource_allocation" and add "lst" item value to it, show above我想创建一个名为“resource_allocation”的新键并向其添加“lst”项值,如上所示

i am trying to do like this:我正在尝试这样做:

x["resource_allocation"] = lst
print(x)

got error:得到错误:

    x["resource_allocation"] = lst
TypeError: list indices must be integers or slices, not str

i am sure i am doing it wrong, can any one suggest me the right way to achieve desired output?我确定我做错了,有人可以建议我实现所需输出的正确方法吗? i have looked SO posts didn't come across this kind of requirements before.我看过SO帖子之前没有遇到过这种要求。 Thanks谢谢

You tried to assign a key value to the list so it's throwing an error.You have to loop through the x list and assign keys individually.您尝试将键值分配给列表,因此引发错误。您必须遍历 x 列表并单独分配键。

for i in range(len(x)):
    x[i]['resource_allocation']=lst[i]
print(json.dumps(x,indent=2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM