python基于公共密钥将字典列表合并为1

plupiseo  于 2023-01-27  发布在  Python
关注(0)|答案(2)|浏览(156)

我有这样的法令:

data= [{'org_id': 'AGO-cbgo', 'ws_name': 'finops_enricher-nonprod', 'ws_id': 'ws-CTvV7QysPeY4Gt1Q', 'current_run': None}
{'org_id': 'AGO-cbgo', 'ws_name': 'finops_enricher-prod', 'ws_id': 'ws-s4inidN9aDxELE4a', 'current_run': None}
{'org_id': 'AGO-cbgo', 'ws_name': 'finops_enricher-preprod', 'ws_id': 'ws-fvyKv7m4FRYf8v5o', 'current_run': None}
{'org_id': 'AGO-cbgo', 'ws_name': 's3_dlp-getd_sherlock-prod', 'ws_id': 'ws-XpzzptzGHL2YNjsL', 'current_run': None}
{'org_id': 'AGO-cbgo', 'ws_name': 's3_dlp-getd_sherlock-nonprod', 'ws_id': 'ws-dksk8nnXTjzLWmRn', 'current_run': 'run-osSNuCtt5ULHPBus'}
]

我需要这个结果:

result= {'AGO-cbgo', 'ws': [
            {'ws_name': 'finops_enricher-nonprod', 'ws_id': 'ws-CTvV7QysPeY4Gt1Q', 'current_run': None},
            {'ws_name': 'finops_enricher-preprod', 'ws_id': 'ws-fvyKv7m4FRYf8v5o', 'current_run': None},
            {'ws_name': 's3_dlp-getd_sherlock-prod', 'ws_id': 'ws-XpzzptzGHL2YNjsL', 'current_run': None},
            {'ws_name': 's3_dlp-getd_sherlock-nonprod', 'ws_id': 'ws-dksk8nnXTjzLWmRn', 'current_run': 'run-osSNuCtt5ULHPBus'}
            ]
        }

有什么想法如何实现这一点吗?我尝试了集合和defaultdict,但没有成功。

s4chpxco

s4chpxco1#

使用集合中的defaultdict:将'ws'作为键,其值作为包含具有不同属性的sum-dict的列表

from collections import defaultdict

res = defaultdict(list)
res["org_id"] = set([item["org_id"] for item in data])
for item in data:
    res["ws"].append({key: value for key, value in item.items() if key != "org_id"})

new_dict = dict(res)
print(new_dict)

您还可以使用以下方法在没有defaultdict的情况下获得相同的输出
使用**zip()**

org_id, ws = zip(*[(item.pop("org_id"), item) for item in data])
new_dict = {"org_id": set(org_id), "ws": list(ws)}
print(new_dict)

或初始化字典:

new_dict = {"org_id": set(), "ws": []}
for item in data:
    new_dict["org_id"].add(item["org_id"])
    new_dict["ws"].append({key: value for key, value in item.items() if key !="org_id"})

print(new_dict)
    • 注意:**你的输出不是一个合适的字典。贝娄是一个合适的输出,我怀疑这是你可能要求的输出。
{'org_id': {'AGO-cbgo'}, 'ws': [
    {'ws_name': 'finops_enricher-nonprod', 'ws_id': 'ws-CTvV7QysPeY4Gt1Q', 'current_run': None}, 
    {'ws_name': 'finops_enricher-prod', 'ws_id': 'ws-s4inidN9aDxELE4a', 'current_run': None}, 
    {'ws_name': 'finops_enricher-preprod', 'ws_id': 'ws-fvyKv7m4FRYf8v5o', 'current_run': None}, 
    {'ws_name': 's3_dlp-getd_sherlock-prod', 'ws_id': 'ws-XpzzptzGHL2YNjsL', 'current_run': None}, 
    {'ws_name': 's3_dlp-getd_sherlock-nonprod', 'ws_id': 'ws-dksk8nnXTjzLWmRn', 'current_run': 'run-osSNuCtt5ULHPBus'}
    ]}

pprint模块打印输出将产生以下格式:

from pprint import pprint 

print(new_dict)
{'org_id': {'AGO-cbgo'},
 'ws': [{'current_run': None,
         'ws_id': 'ws-CTvV7QysPeY4Gt1Q',
         'ws_name': 'finops_enricher-nonprod'},
        {'current_run': None,
         'ws_id': 'ws-s4inidN9aDxELE4a',
         'ws_name': 'finops_enricher-prod'},
        {'current_run': None,
         'ws_id': 'ws-fvyKv7m4FRYf8v5o',
         'ws_name': 'finops_enricher-preprod'},
        {'current_run': None,
         'ws_id': 'ws-XpzzptzGHL2YNjsL',
         'ws_name': 's3_dlp-getd_sherlock-prod'},
        {'current_run': 'run-osSNuCtt5ULHPBus',
         'ws_id': 'ws-dksk8nnXTjzLWmRn',
         'ws_name': 's3_dlp-getd_sherlock-nonprod'}]}
vcirk6k6

vcirk6k62#

这样就可以完成任务,而不会改变原始数据:

from pprint import pprint

data = [
    {'org_id': 'AGO-cbgo', 'ws_name': 'finops_enricher-nonprod', 'ws_id': 'ws-CTvV7QysPeY4Gt1Q', 'current_run': None},
    {'org_id': 'AGO-cbgo', 'ws_name': 'finops_enricher-prod', 'ws_id': 'ws-s4inidN9aDxELE4a', 'current_run': None},
    {'org_id': 'AGO-cbgo', 'ws_name': 'finops_enricher-preprod', 'ws_id': 'ws-fvyKv7m4FRYf8v5o', 'current_run': None},
    {'org_id': 'AGO-cbgo', 'ws_name': 's3_dlp-getd_sherlock-prod', 'ws_id': 'ws-XpzzptzGHL2YNjsL', 'current_run': None},
    {'org_id': 'AGO-cbgo', 'ws_name': 's3_dlp-getd_sherlock-nonprod', 'ws_id': 'ws-dksk8nnXTjzLWmRn', 'current_run': 'run-osSNuCtt5ULHPBus'},
]

new_data = {}

for d in data:
    # copy the dict to prevent mutating the original data
    d_copy = dict(**d)
    d_copy.pop('org_id')
    # add the copied dict to the output, creating keys as needed
    new_data.setdefault(d['org_id'], []).append(d_copy)

pprint(new_data)

结果:

{'AGO-cbgo': [{'current_run': None,
               'ws_id': 'ws-CTvV7QysPeY4Gt1Q',
               'ws_name': 'finops_enricher-nonprod'},
              {'current_run': None,
               'ws_id': 'ws-s4inidN9aDxELE4a',
               'ws_name': 'finops_enricher-prod'},
              {'current_run': None,
               'ws_id': 'ws-fvyKv7m4FRYf8v5o',
               'ws_name': 'finops_enricher-preprod'},
              {'current_run': None,
               'ws_id': 'ws-XpzzptzGHL2YNjsL',
               'ws_name': 's3_dlp-getd_sherlock-prod'},
              {'current_run': 'run-osSNuCtt5ULHPBus',
               'ws_id': 'ws-dksk8nnXTjzLWmRn',
               'ws_name': 's3_dlp-getd_sherlock-nonprod'}]}

相关问题