我有2份字典
[
{'company': 'XYZ', 'url': '/xyz', 'industry': ['Electric utility', 'Utilities'], 'sector': 'Conventional electricity', 'website': ['xyz.com']},
{...}
]
每个字典都有一个公用键“ url”。
我的要求是将所有列表中dict中具有相同值的“ url”键进行合并,如果合并时dict中不存在特定键,则为这些键分配None / null。
所需的输出应为:
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']}, {...}]
我得到的输出是:
{{1}}
感谢您的帮助。最好的pythonic方式会很棒。 预先感谢
答案 0 :(得分:2)
您可以使用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']}]