简体   繁体   English

当它们的键具有值列表时合并多个字典

[英]Merge multiple dictionaries when their keys have a list of values Python

I came across a post that had a complete and correct way of merging two dictionaries, when each dictionary has for each key a list of values. 我碰到过一篇帖子,其中有完整而正确的方法来合并两个字典,而每个字典的每个键都有一个值列表。

The input of the program is something like this: 该程序的输入如下所示:

d1:  {'apple': ['5', '65'], 'blue': ['9', '10', '15', '43'], 'candle': ['15'], 'is': ['5', '6', '13', '45', '96'], 'yes': ['1', '2', '3', '11'], 'zone': ['5', '6', '9', '10', '12', '14', '18', '19', '23', '24', '25', '29', '45']}
d2:  {'apple': ['43', '47'], 'appricote': ['1', '2', '3', '4', '5', '6'], 'candle': ['1', '2', '4', '5', '6', '9'], 'delta': ['14', '43', '47'], 'dragon': ['23', '24', '25', '26'], 'eclipse': ['11', '13', '15', '19'], 'island': ['1', '34', '35']}

code of merge function is: 合并功能的代码为:

def merge_dictionaries(dict1, dict2):

result = {}
new_result = {}
for key in set().union(*(dict1, dict2)):
    result[key] = sorted(dict1.get(key, []) + dict2.get(key, []), key=int)
    # Delete any double value for each key
    new_result[key] = [ii for n, ii in enumerate(result[key]) if ii not in result[key][:n]]

return new_result

With result: 结果:

Merged:  {'is': ['5', '6', '13', '45', '96'], 'dragon': ['23', '24', '25', '26'], 'apple': ['5', '43', '47', '65'], 'appricote': ['1', '2', '3', '4', '5', '6'], 'delta': ['14', '43', '47'], 'eclipse': ['11', '13', '15', '19'], 'yes': ['1', '2', '3', '11'], 'zone': ['5', '6', '9', '10', '12', '14', '18', '19', '23', '24', '25', '29', '45'], 'blue': ['9', '10', '15', '43'], 'island': ['1', '34', '35'], 'candle': ['1', '2', '4', '5', '6', '9', '15']}

What I want to achieve now is to extend this way for merging a dynamic number of dictionaries of this kind.This is my try in which I get from the list of "dicts" 2 items each time and try to merge them. 我现在想要实现的是扩展这种方式来合并动态数量的此类字典。这是我的尝试,每次尝试从“字典”列表中获取2个项目,然后尝试将其合并。

def merge_multiple_dictionaries(dicts):
''' This is just to print the input I get
l = len(dicts)
print("Len of dicts: ", l)
for j in range(0, l):
    print("j is: ", dicts[j])
'''
result = {}
new_result = {}
for k in range(0, l):
     if k is (len(dicts)-1):
        break
    print("k: ", k)
    for key in set().union(*(dicts[k], dicts[k+1])):
        result[key] = sorted(dicts[k].get(key, []) + dicts[k+1].get(key, []), key=int)
        # Delete any double value for each key
        new_result[key] = [ii for n, ii in enumerate(result[key]) if ii not in result[key][:n]]
# result = OrderedDict(sorted([(k, v) for k, v in result.items()]))

return new_result

So is there a more pythonic way to achieve that? 那么,有没有更Python的方法来实现这一目标?

您有一个错字:您用dict表示dicts

There is no need to iterate over pairs of dictionaries, as set().union() operation can take multiple arguments: 无需迭代对字典,因为set()。union()操作可以采用多个参数:

def merge_dictionaries(*args):
    result = {}
    for key in set().union(*args):
        # let's convert the value union to set before sorting to get rid of the duplicates
        result[key] = sorted(set.union(*[set(d.get(key, [])) for d in args]), key=int)
    return result

暂无
暂无

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

相关问题 使用多个键合并python字典列表 - Merge list of python dictionaries using multiple keys 将Python列表中的键值与多个词典进行比较 - Compare values of keys in Python list with multiple dictionaries 将两个列表合并成字典列表,并在python中使用多个键 - Merge two list into a list of dictionaries with multiple keys in python 当我想找到多个字典的多个键具有多个相同值时,如何在python中的字典列表中进行搜索? - How do I search through a list of dictionaries in python when I want to find multiple dictionaries which have multipe same values for multiple keys? Python-合并两个字典列表,添加重复键的值 - Python - merge two list of dictionaries adding values of repeated keys Python 3:创建多个字典的列表具有相同的键但来自多个列表的不同值 - Python 3: Creating list of multiple dictionaries have same keys but different values coming from multiple lists 如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典? - How can I split a python dictionary with multiple text-list values to have separate dictionaries of keys that have the same values? Python-合并字典,添加重复键的值 - Python - merge dictionaries adding values of repeated keys 通过键合并两个Python字典的值 - Merge values of two Python dictionaries by keys Python - 将两个词典合并为一个列表(按键) - Python - Merge two dictionaries into a single list (By Keys)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM