简体   繁体   English

如何使用内部键:值对将字典列表转换为字典字典

[英]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.

相关问题 将带有字典的字典转换为具有相同键值对的列表 - convert dictionaries with dictionary into list with same key value pair 如何使用我想要获取的字典的一个键/值对从字典列表中访问字典 - How to access the dictionary from a list of dictionaries using one key/value pair of the dictionary that I want to fetch 根据键值对更新字典列表中的整个字典 - update an entire dictionary in a list of dictionaries based on key value pair 如何将字典列表附加到字典中的键值? - How to append a list of dictionaries to the value of a key in a dictionary? 如何在Python中将字典列表转换为字典词典 - How convert list of dictionaries to a dictionary of dictionaries in Python 如何将键/值对插入词典列表中包含的每个词典中 - How can I insert a key/value pair into each dictionary contained in a list of dictionaries 从字典的字典中,返回内部字典的列表,用键更新 - from a dictionary of dictionaries, return a list of the inner dictionaries, updated with the key 字典列表 Python:如果现有的键、值对匹配,则将新的键、值对附加到字典中 - List of Dictionaries Python: Append a new key, value pair to dictionary if existing key, value pair matches 根据匹配的键值对组合字典列表中的字典 - combine dictionaries in list of dictionaries based on matching key:value pair 在字典列表中添加键/值对(使用计算) - Add a key/value pair(With caculation) in a list of dictionaries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM