如何将多个字典的列表合并到列表字典中? [英] How to merge a list of multiple dictionaries into a dictionary of lists?

查看:2554
本文介绍了如何将多个字典的列表合并到列表字典中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python3.x中有以下词典列表:

I have the following list of dictionaries in Python3.x:

list_of_dictionaries = [{0:3523, 1:3524, 2:3540, 4:3541, 5:3542}, 
                        {0:7245, 1:7246, 2:7247, 3:7248, 5:7249, 6:7250},
                        {1:20898, 2:20899, 3:20900, 4:20901, 5:20902}]

在这种情况下,它是包含三个词典的单个列表.

In this case, it's a single list with three dictionaries.

我想将其有效地合并到一个以列表为值的字典中;这是正确的答案:

I would like to efficiently merge this into a single dictionary with lists as values; here is the correct answer:

correct = {0:[3523, 7245], 1:[3524, 7246, 20898], 2:[3540, 7247, 20899], 
               3:[7248, 20900], 4:[3541, 20901], 5:[3542, 7249, 20902], 6:[7250]}

我的第一个念头是这样的列表理解:

My first thought was a list comprehension like this:

dict(pair for dictionary in list_of_dictionaries for pair in dictionary.items())

但这是错误的,因为它不包含值列表:

But this is wrong, as it doesn't include lists of values:

{0: 7245, 1: 20898, 2: 20899, 4: 20901, 5: 20902, 3: 20900, 6: 7250}

我也担心如何有效地创建价值列表.它也可能无法缩放到大型列表/大型词典.

I'm also worried about how to efficiently as possible create value lists. It may not scale to large lists/large dictionaries either.

我该怎么做?

推荐答案

defaultdict

您可以使用collections.defaultdict.由于您没有定义任何列表,因此无法理解词典.这可能比使用字典理解更为有效,后者将涉及为每个唯一键迭代每个字典.

defaultdict

You can use collections.defaultdict. Your dictionary comprehension will never work as you are not defining any lists. This is likely to be more efficient than using a dictionary comprehension, which would involve iterating each dictionary for each unique key.

from collections import defaultdict

dd = defaultdict(list)

for d in list_of_dictionaries:
    for k, v in d.items():
        dd[k].append(v)

结果:

print(dd)

defaultdict(list,
            {0: [3523, 7245],
             1: [3524, 7246, 20898],
             2: [3540, 7247, 20899],
             4: [3541, 20901],
             5: [3542, 7249, 20902],
             3: [7248, 20900],
             6: [7250]})

字典理解

可能理解 字典,但这需要计算键的并集并为这些键中的每一个迭代字典列表:

Dictionary comprehension

A dictionary comprehension is possible but this requires calculating the union of keys and iterating the list of dictionaries for each of these keys:

allkeys = set().union(*list_of_dictionaries)

res = {k: [d[k] for d in list_of_dictionaries if k in d] for k in allkeys}

{0: [3523, 7245],
 1: [3524, 7246, 20898],
 2: [3540, 7247, 20899],
 3: [7248, 20900],
 4: [3541, 20901],
 5: [3542, 7249, 20902],
 6: [7250]}

时间复杂度

请考虑以下条款:

Time complexity

Consider these terms:

n = sum(map(len, list_of_dictionaries))
m = len(set().union(*list_of_dictionaries))
k = len(list_of_dictionaries)

在这种情况下,defaultdict解决方案的复杂度为O( n ),而字典理解的复杂度为O( mk ),其中 mk > = n .

In this context, the defaultdict solution will have complexity O(n), while the dictionary comprehension will have complexity O(mk), where mk >= n.

这篇关于如何将多个字典的列表合并到列表字典中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆