[英]How to convert list of dictionaries into a dictionary of dictionaries by using inner key:value pair
So, instead of trying to explain things first, I will just show you what I have and what I want (this is easier):因此,与其尝试先解释事情,不如向您展示我拥有的和想要的(这更容易):
What I have:我拥有的:
dict_list = [
{'some': 1.2, 'key': 1.3, 'words': 3.9, 'label': 0},
{'other': 1.2, 'wordly': 1.3, 'words': 3.9, 'label': 1},
{'other': 10, 'work': 1.3, 'like': 3.9, 'label': 1},
]
What I want to get from what I have:我想从我所拥有的中得到什么:
dict_dict = { "0":{'some': 1.2, 'key': 1.3, 'words': 3.9},
"1":{'other': 10, 'wordly': 1.3, 'work': 1.3, 'like': 3.9, 'words': 3.9},
}
Explanation:解释:
So, I want to create a dictionary by using the " label
" keys as the main keys in that new dictionary.所以,我想通过使用“
label
”键作为新字典中的主键来创建一个字典。 I also need to merge dictionaries that have the same label.我还需要合并具有相同标签的字典。 During this merging, I need to keep the highest value if there is a duplicate key (as the "
other
" key in the example).在此合并过程中,如果存在重复键(如示例中的“
other
”键),我需要保留最高值。
Why don't I do all of this before I create the original list of dicts?为什么我不在创建原始字典列表之前完成所有这些工作?
Because dict_list
is a result of a joblib (multiprocessing) process.因为
dict_list
是 joblib(多处理)进程的结果。 Sharing some objects between processes slowing down the multiprocessing.在进程之间共享一些对象会减慢多处理速度。 So, instead of sharing, I have decided to run the heavy work on multiple cores and then do the organizing after.
因此,我决定在多个内核上运行繁重的工作,然后再进行组织,而不是共享。 I am not sure if this approach will be any helpful but I can't know without testing.
我不确定这种方法是否有帮助,但不经过测试我就无法知道。
Counter module has nice merging feature a|b
which joins the dictionaries keeping the higher values. Counter 模块具有很好的合并功能
a|b
,它加入了保持较高值的字典。
from collections import Counter
dict_dict = {}
for dictionary in dict_list:
label = str(dictionary.pop('label'))
dict_dict[label] = dict_dict.get(label,Counter())|Counter(dictionary)
###If you don't need Counters, just convert back to dictionaries
dict_dict = {i:dict(v) for i,v in dict_dict.items()}
easy pisy:简单的 pisy:
dict_of_dicts = {i:item for i,item in enumerate(list_of_dicts)}
if u insist on strings in the keys:如果你坚持在键中使用字符串:
dict_of_dicts = {str(i):item for i,item in enumerate(list_of_dicts)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.