简体   繁体   English

基于键值拆分嵌套字典

[英]splitting nested dictionary based on a key values

Would anyone be able to give me a tip on how to split a nested dictionary into separate nested dictionaries based on a common key name called score and the value of score ?会有人能够给我基于一个公共密钥名称叫上如何分割嵌套的字典为单独的嵌套字典小费score和价值score

For example break up this nested dictionary nested_group_map_original :例如分解这个嵌套的字典nested_group_map_original

nested_group_map_original = {
    'group_l1n': {'score': 0.12949562072753906, 'VMA-1-1': '14', 'VMA-1-2': '13', 'VMA-1-3': '15', 'VMA-1-4': '11', 'VMA-1-5': '9', 'VMA-1-7': '7', 'VMA-1-10': '21', 'VMA-1-11': '16'}, 
    'group_l1s': {'score': -0.40303707122802734, 'VMA-1-6': '8', 'VMA-1-8': '6', 'VMA-1-9': '10', 'VMA-1-12': '19', 'VMA-1-13': '20', 'VMA-1-14': '37', 'VMA-1-15': '38', 'VMA-1-16': '39'}, 
    'group_l2n': {'score': 0.6091512680053768, 'VAV-2-1': '12032', 'VAV-2-2': '12033', 'VMA-2-3': '31', 'VMA-2-4': '29', 'VAV-2-5': '12028', 'VMA-2-6': '27', 'VMA-2-7': '30', 'VMA-2-12': '26'}, 
    'group_l2s': {'score': 0.11078681945799929, 'VMA-2-8': '34', 'VAV-2-9': '12035', 'VMA-2-10': '36', 'VMA-2-11': '25', 'VMA-2-13': '23', 'VMA-2-14': '24'}
               }

Make it look like this below but in a programmatic way for two separate nested dictionaries named nested_group_map_copy_lows and nested_group_map_copy_high :使它看起来像下面这样,但以编程方式为两个名为nested_group_map_copy_lowsnested_group_map_copy_high独立嵌套字典:

nested_group_map_copy_lows = {
    'group_l1s': {'score': -0.40303707122802734, 'VMA-1-6': '8', 'VMA-1-8': '6', 'VMA-1-9': '10', 'VMA-1-12': '19', 'VMA-1-13': '20', 'VMA-1-14': '37', 'VMA-1-15': '38', 'VMA-1-16': '39'}, 
    'group_l2s': {'score': 0.11078681945799929, 'VMA-2-8': '34', 'VAV-2-9': '12035', 'VMA-2-10': '36', 'VMA-2-11': '25', 'VMA-2-13': '23', 'VMA-2-14': '24'}
                   }

nested_group_map_copy_highs = {
    'group_l2n': {'score': 0.6091512680053768, 'VAV-2-1': '12032', 'VAV-2-2': '12033', 'VMA-2-3': '31', 'VMA-2-4': '29', 'VAV-2-5': '12028', 'VMA-2-6': '27', 'VMA-2-7': '30', 'VMA-2-12': '26'},
    'group_l1n': {'score': 0.12949562072753906, 'VMA-1-1': '14', 'VMA-1-2': '13', 'VMA-1-3': '15', 'VMA-1-4': '11', 'VMA-1-5': '9', 'VMA-1-7': '7', 'VMA-1-10': '21', 'VMA-1-11': '16'},     
                   }

Not really sure how to tackle this, I think I need to use enumerate to create entire new dictionaries but if I try to find highest scores in a separate list scores_不太确定如何解决这个问题,我想我需要使用enumerate来创建整个新词典,但是如果我尝试在单独的列表中找到最高分scores_

scores_ = []
for i in nested_group_map_original:
    scores_.append(nested_group_map_original[i]["score"])
    
scores_sorted = sorted(scores_, key = float)

Then slice for highest and lowest values:然后切片最高和最低值:

scores_sorted_highs = scores_sorted[2:]
scores_sorted_lows = scores_sorted[:2]

I am stuck here I dont think del is way to go, any tips greatly appreciated... I know in my code I am not even defining new dictionaries which I think I could do with Python enumerate but not sure how to implement that我被困在这里我不认为del是要走的路,任何提示都非常感谢......我知道在我的代码中我什至没有定义新的字典,我认为我可以用 Python enumerate但不知道如何实现它

for i in nested_group_map_original:
    if nested_group_map_original[i]["score"] in scores_sorted_highs:
        del nested_group_map_original[i]

This errors out:这个错误:

RuntimeError: dictionary changed size during iteration

You can sort the keys of the original dictionary according to their corresponding scores, like so:您可以根据对应的分数对原始字典的键进行排序,如下所示:

sorted_keys = sorted(nested_group_map_original, key = lambda x: nested_group_map_original[x]['score'])

You can then split into two different dictionaries according to how many values you want in each.然后,您可以根据每个字典中想要的值数量将其拆分为两个不同的字典。 Like in your example of two, you could do the following:就像在您的两个示例中一样,您可以执行以下操作:

scores_sorted_lows = {k:nested_group_map_original[k] for k in sorted_keys[:2]}
scores_sorted_highs = {k:nested_group_map_original[k] for k in sorted_keys[2:]}

If the dict isn't super huge, then one easy way to do this would just be to construct the list by filtering the map twice using a comprehension:如果字典不是超级大,那么一种简单的方法就是通过使用理解过滤地图两次来构建列表:

nested_group_map_low = {k:v for k,v in nested_group_map_original.items() if is_low_score(v["score"])}
nested_group_map_high = {k:v for k,v in nested_group_map_original.items() if not is_low_score(v["score"])}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM