Sha*_*ang 3 python performance dictionary python-3.x dictionary-comprehension
我在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}]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它是一个包含三个词典的单个列表.
我想有效地将它合并到一个单词字典中,列表作为值; 这是正确的答案:
correct = {0:[3523, 7245], 1:[3524, 7246, 20898], 2:[3540, 7247, 20899],
3:[7248, 20900], 4:[3541, 20901], 5:[3542, 7249, 20902], 6:[7250]}
Run Code Online (Sandbox Code Playgroud)
我的第一个想法是这样的列表理解:
dict(pair for dictionary in list_of_dictionaries for pair in dictionary.items())
Run Code Online (Sandbox Code Playgroud)
但这是错误的,因为它不包括值列表:
{0: 7245, 1: 20898, 2: 20899, 4: 20901, 5: 20902, 3: 20900, 6: 7250}
Run Code Online (Sandbox Code Playgroud)
我也担心如何有效地创建价值表.它也可能无法扩展到大型列表/大型词典.
我怎么能做到这一点?
defaultdict你可以用collections.defaultdict.由于您没有定义任何列表,因此您的字典理解将永远不会起作用.这可能比使用字典理解更有效,字典理解涉及为每个唯一键迭代每个字典.
from collections import defaultdict
dd = defaultdict(list)
for d in list_of_dictionaries:
for k, v in d.items():
dd[k].append(v)
Run Code Online (Sandbox Code Playgroud)
结果:
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]})
Run Code Online (Sandbox Code Playgroud)
字典理解是可能的,但这需要计算键的并集并迭代每个键的字典列表:
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]}
Run Code Online (Sandbox Code Playgroud)
请考虑以下条款:
n = sum(map(len, list_of_dictionaries))
m = len(set().union(*list_of_dictionaries))
k = len(list_of_dictionaries)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,defaultdict解决方案将具有复杂度O(n),而字典理解将具有复杂度O(mk),其中mk > = n.