gpt4 book ai didi

python - 合并公共(public)键中具有相同值的多个字典列表

转载 作者:行者123 更新时间:2023-12-01 00:22:03 25 4
gpt4 key购买 nike

我有 2 个字典列表

l1 = [{'company': 'XYZ', 'url': '/xyz', 'industry': 'Utilities', 'sector': 'Conventional electricity'}, {...}]
l2 = [{'url': '/xyz', 'industry': ['Electric utility'], 'website': ['xyz.com']}, {...}]

每个字典都有一个公共(public)键“url”。

我的要求是应该合并所有列表中字典中具有相同值的“url”键,如果合并时字典中不存在特定键,则为这些键分配 None/null 。

期望的输出应该是:

[
{'company': 'XYZ', 'url': '/xyz', 'industry': ['Electric utility', 'Utilities'], 'sector': 'Conventional electricity', 'website': ['xyz.com']},
{...}
]

到目前为止我尝试过的是:

from itertools import groupby
from collections import ChainMap
from operator import itemgetter
from pprint import pprint


def merge_lists_of_dicts(list1, list2, by_key):
dict_list = list1 + list2
by_key = itemgetter(by_key)
res = map(lambda dict_tuple: dict(ChainMap(*dict_tuple[1])),
groupby(sorted(dict_list, key=by_key), key=by_key))

return list(res)

pprint(merge_lists_of_dicts(l1, l2, "url"))

我得到的输出是:

 [{'company': 'XYZ',
'industry': 'Utilities',
'sector': 'Internet',
'url': '/xyz',
'website': ['xyz.com']}, {...}]

感谢任何帮助。最好的 pythonic 方式会很棒。提前致谢

最佳答案

您可以使用itertools.groupby,然后应用自定义合并函数:

from itertools import groupby as gb
l1 = [{'company': 'XYZ', 'url': '/xyz', 'industry': 'Utilities', 'sector': 'Conventional electricity'}]
l2 = [{'url': '/xyz', 'industry': ['Electric utility'], 'website': ['xyz.com']}]
def merge(d, url):
keys = [i for b in d for i in b]
new_d = {i:(lambda x:None if not x else x)([b[i] for b in d if i in b]) for i in keys}
_d = {a:b if b is None else b[0] if len(b) == 1 else [j for k in b for j in ([k] if not isinstance(k, list) else k)] for a, b in new_d.items()}
return {**_d, 'url':url}

newl = [merge(list(b), a) for a, b in gb(sorted(l1+l2, key=lambda x:x['url']), key=lambda x:x['url'])]

输出:

[{'company': 'XYZ', 'url': '/xyz', 'industry': ['Utilities', 'Electric utility'], 'sector': 'Conventional electricity', 'website': ['xyz.com']}]

关于python - 合并公共(public)键中具有相同值的多个字典列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58892086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com