[英]For a list of lists, merge the dictionaries with collections.defaultdict
This is a simple question, but I'm confused by the behavior of collections.defaultdict
. 这是一个简单的问题,但我对collections.defaultdict
的行为感到困惑。 This is to help me learn how this works. 这是为了帮助我了解它是如何工作的。
This question is an extrapolation from this useful question: How to merge a list of multiple dictionaries into a dictionary of lists? 这个问题是从这个有用的问题推断出来的: 如何将多个词典列表合并到列表中?
Let's now say I have a list of lists of dictionaries. 我们现在说我有一个词典列表清单。 I want to merge the dictionaries as detailed in the above question: 我想合并上面问题中详述的字典:
list_of_dictionaries2 = [[{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}], [{0:3, 1:4, 2:5, 3:6}]]
The intended answer is this: 预期的答案是这样的:
correct2 = [[{0:[3523, 7245], 1:[3524, 7246, 20898], 2:[3540, 7247, 20899],
3:[7248, 20900], 4:[3541, 20901], 5:[3542, 7249, 20902], 6:[7250]}],
[{0:3, 1:4, 2:5, 3:6}]]
Previously, for a single list of dictionaries, we solved this by creating an empty dictionary with default values as lists, ie we used collections.defaultdict(list)
. 以前,对于单个字典列表,我们通过创建一个默认值为列表的空字典来解决这个问题,即我们使用collections.defaultdict(list)
。
Given this case is a list of lists, I thought another for loop would be the solution, appending dictionaries into an empty list: 鉴于这种情况是一个列表列表,我认为另一个for循环将是解决方案,将字典附加到一个空列表:
from collections import defaultdict
correct2 = defaultdict(list)
empty = []
for smaller_list in list_of_dictionaries2:
for d in smaller_list:
for k,v in d.items():
correct2[k].append(v)
empty.append(correct2)
This is very wrong. 这是非常错误的。
>>> print(empty)
[defaultdict(<class 'list'>, {0: [3523, 7245, 3], 1: [3524, 7246, 20898, 4],
2: [3540, 7247, 20899, 5], 4: [3541, 20901], 5: [3542, 7249, 20902],
3: [7248, 20900, 6], 6: [7250]}), defaultdict(<class 'list'>,
{0: [3523, 7245, 3], 1: [3524, 7246, 20898, 4], 2: [3540, 7247, 20899, 5],
4: [3541, 20901], 5: [3542, 7249, 20902], 3: [7248, 20900, 6], 6: [7250]})]
It looks like all dictionaries were combined. 看起来所有词典都是合并的。 And there are two copies. 并且有两份副本。 This is not what I want. 这不是我想要的。
How do I do this for each individual list, like above? 如何对每个列表执行此操作,如上所述? Where am I mistaken in my understanding? 我在哪里弄错了?
You don't actually have a list of dictionaries, but a list of lists of dictionaries, and you're trying to merge the dictionaries within the sub-lists, so you should initialize your defaultdict
inside the loop that iterates through the main list: 你实际上没有字典列表,但是列出了字典列表,并且你正在尝试合并子列表中的字典,所以你应该在遍历主列表的循环中初始化你的defaultdict
:
empty = []
for smaller_list in list_of_dictionaries2:
correct2 = defaultdict(list)
for d in smaller_list:
for k,v in d.items():
correct2[k].append(v)
empty.append(correct2)
empty
would become: empty
将成为:
[defaultdict(<class 'list'>, {0: [3523, 7245], 1: [3524, 7246, 20898], 2: [3540, 7247, 20899], 4: [3541, 20901], 5: [3542, 7249, 20902], 3: [7248, 20900], 6: [7250]}), defaultdict(<class 'list'>, {0: [3], 1: [4], 2: [5], 3: [6]})]
Note that your expected output for the second merged defaultdict
is incorrect since the value of each key should be a list after merge. 请注意,第二个合并的defaultdict
预期输出不正确,因为每个键的值应该是合并后的列表。 Also the list of lists of dictionary should become a list of defaultdict
s of lists after the merge, not a list of lists of defaultdict
s of lists. 此外,字典列表列表应该成为合并后列表的defaultdict
列表,而不是列表的defaultdict
列表列表。
you should initialize correct2
every loop like this 你应该像这样初始化每个循环的correct2
for smaller_list in list_of_dictionaries2:
correct2 = defaultdict(list)
for d in smaller_list:
for k, v in d.items():
correct2[k].append(v)
empty.append(correct2)
out put will be out put将是
[defaultdict(<class 'list'>, {0: [3523, 7245], 1: [3524, 7246, 20898],
2: [3540, 7247, 20899], 4: [3541, 20901], 5: [3542, 7249, 20902],
3: [7248, 20900], 6: [7250]}),
defaultdict(<class 'list'>, {0: [3],1: [4], 2: [5], 3: [6]})]
note that defaultdict is mutable Objects 请注意,defaultdict是可变对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.