简体   繁体   English

如何使用更新的键和值从旧的嵌套字典创建新的嵌套字典?

[英]How can I create a new nested dictionary from an old nested dictionary with updated keys and value?

Currently, I am trying to create a new dictionary from an unusable dictionary but am having difficulty updating/creating new keys based on the number of tuples in the 'Acceptable' key.目前,我正在尝试从无法使用的字典创建新字典,但是根据“可接受”键中的元组数量更新/创建新键时遇到困难。

dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'),
                    'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')},
                    'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')},
                    'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')},
                    'COW': {'Acceptable': ([[87, 69]], 'COW_GROUP'), 'Unacceptable': ([], 'COW_GROUP')}}

new_dict = {}
for key, values in dirty_dictionary.items():
    if len(values['Acceptable'][0]) == 0:
        new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': []}
    for oids in values['Acceptable'][0]:
        if len(values['Acceptable'][0]) == 1:
            new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': oids}
        if len(values['Acceptable'][0]) > 1:
            for i in range(len(values['Acceptable'][0])):
                new_dict[dirty_dictionary[key]['Acceptable'][-1] + F'_{i}'] = {'Acceptable': values['Acceptable'][0][i]}

    # for oids in values['Unacceptable'][0]:
    #     if len(values['Unacceptable'][0]) == 1:
    #         new_dict[dirty_dictionary[key]['Unacceptable'][-1]].update({'Unacceptable': oids})
    #     if len(values['Unacceptable'][0]) > 1:
    #         for i in range(len(values['Unacceptable'][0])):
    #             new_dict[dirty_dictionary[key]['Unacceptable'][-1] + F'_{i}'].update({'Unacceptable': values['Unacceptable'][0][i]})

print(new_dict)

I can create a new dictionary with all 'Acceptable' Keys/Values, but I am stuck on updating the dictionary with the 'Unacceptable' since new groups need to be created if the len of values['Acceptable'][0] > 1.我可以创建一个包含所有“可接受”键/值的新字典,但我坚持使用“不可接受”更新字典,因为如果值的 len 值 ['Acceptable'][0] > 1,则需要创建新组.

The goal is to get the final dictionary to look like:目标是让最终的字典看起来像:

final = {'DOG_GROUP': {'Acceptable': [35, 38], 'Unacceptable': [2, 29, 44, 50, 54, 60]},
         'CAT_GROUP_0': {'Acceptable': [3, 6], 'Unacceptable': []},
         'CAT_GROUP_1': {'Acceptable': [100, 101], 'Unacceptable': [12, 18, 45, 51, 61]},
         'FISH_GROUP': {'Acceptable': [], 'Unacceptable': [13, 19, 22, 28, 34]},
         'COW_GROUP': {'Acceptable': [87, 69], 'Unacceptable': []}}

Try this one.试试这个。

dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'), 'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')}, 'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')}, 'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')}}

new_dict = {}

# assign text strings in variable to get suggestion in IDE
acceptable = 'Acceptable'
unacceptable = 'Unacceptable'

for key, values in dirty_dictionary.items():
    group = values[acceptable][1]
    if len(values[acceptable][0]) <= 1:
        new_dict[group] = {}
        new_dict[group][acceptable] = [y for x in values[acceptable][0] for y in x]
        new_dict[group][unacceptable] = [y for x in values[unacceptable][0] for y in x]
    else:
        for idx, item in enumerate(values[acceptable][0]):
            group_temp = group + '_' + str(idx+1)
            new_dict[group_temp] = {}
            new_dict[group_temp][acceptable] = item
            # if last item then give all unacceptable as a single array
            if idx == len(values[acceptable][0]) - 1:
                new_dict[group_temp][unacceptable] = [y for x in values[unacceptable][0] for y in x]
            else: # else empty array
                new_dict[group_temp][unacceptable] = []

print(new_dict)

This should work as described:这应该像描述的那样工作:

dirty_dictionary = {'DOG': {'Acceptable': ([[35, 38]], 'DOG_GROUP'),
                    'Unacceptable': ([[2], [29], [44], [50], [54], [60]], 'DOG_GROUP')},
                    'CAT': {'Acceptable': ([[3, 6], [100, 101]], 'CAT_GROUP'), 'Unacceptable': ([[12], [18], [45], [51], [61]], 'CAT_GROUP')},
                    'FISH': {'Acceptable': ([], 'FISH_GROUP'), 'Unacceptable': ([[13], [19], [22], [28], [34]], 'FISH_GROUP')}}

new_dict = {}
for _, next_group in dirty_dictionary.items():
    next_group_name = next_group['Acceptable'][1]
    if len(next_group['Acceptable'][0]) > 1: # Split the Acceptables across several keys
        for i, next_acceptable in enumerate(next_group['Acceptable'][0]):
            new_dict[f"{next_group_name}_{i}"] = {'Acceptable': next_acceptable, 'Unacceptable': []}
        new_dict[f'{next_group_name}_{i}']['Unacceptable'] = [next_entry for unacceptable in next_group['Unacceptable'][0] for next_entry in unacceptable]
    else: # Nothing else to consider
        new_dict[f'{next_group_name}'] = {
            'Acceptable': [next_entry for acceptable in next_group['Acceptable'][0] for next_entry in acceptable],
            'Unacceptable': [next_entry for unacceptable in next_group['Unacceptable'][0] for next_entry in unacceptable]
        }

for k, v in new_dict.items():
    print(f'{k}: {v}')

Returns the following result:返回以下结果:

DOG_GROUP: {'Acceptable': [35, 38], 'Unacceptable': [2, 29, 44, 50, 54, 60]}
CAT_GROUP_0: {'Acceptable': [3, 6], 'Unacceptable': []}
CAT_GROUP_1: {'Acceptable': [100, 101], 'Unacceptable': [12, 18, 45, 51, 61]}
FISH_GROUP: {'Acceptable': [], 'Unacceptable': [13, 19, 22, 28, 34]}

Test data:测试数据:

dirty_dictionary = {
    "DOG": {
        "Acceptable": ([[35, 38]], "DOG_GROUP"),
        "Unacceptable": ([[2], [29], [44], [50], [54], [60]], "DOG_GROUP"),
    },
    "CAT": {
        "Acceptable": ([[3, 6], [100, 101]], "CAT_GROUP"),
        "Unacceptable": ([[12], [18], [45], [51], [61]], "CAT_GROUP"),
    },
    "FISH": {
        "Acceptable": ([], "FISH_GROUP"),
        "Unacceptable": ([[13], [19], [22], [28], [34]], "FISH_GROUP"),
    },
    "COW": {"Acceptable": ([[87, 69]], "COW_GROUP"), "Unacceptable": ([], "COW_GROUP")},
}

Solution:解决方案:

import itertools
flatten = itertools.chain.from_iterable

new_dict = {}
for key, value in dirty_dictionary.items():
    accept_status = {"Acceptable": [], "Unacceptable": []}
    if value["Acceptable"][0]:
        accept_status["Acceptable"] = list(flatten(value["Acceptable"][0]))
    if value["Unacceptable"][0]:
        accept_status["Unacceptable"] = list(flatten(value["Unacceptable"][0]))
    new_dict[key] = accept_status

Output:输出:

{'DOG': {'Acceptable': [35, 38], 'Unacceptable': [2, 29, 44, 50, 54, 60]},
 'CAT': {'Acceptable': [3, 6, 100, 101], 'Unacceptable': [12, 18, 45, 51, 61]},
 'FISH': {'Acceptable': [], 'Unacceptable': [13, 19, 22, 28, 34]},
 'COW': {'Acceptable': [87, 69], 'Unacceptable': []}}

because you create keys from Acceptable try this code :因为您从 Acceptable 创建密钥,请尝试以下代码:

new_dict = {}
for key, values in dirty_dictionary.items():
    if len(values['Acceptable'][0]) == 0:
        new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': []}
for oids in values['Acceptable'][0]:
    if len(values['Acceptable'][0]) == 1:
        new_dict[dirty_dictionary[key]['Acceptable'][-1]] = {'Acceptable': oids}
    if len(values['Acceptable'][0]) > 1:
        for i in range(len(values['Acceptable'][0])):
            new_dict[dirty_dictionary[key]['Acceptable'][-1] + F'_{i}'] = {'Acceptable': values['Acceptable'][0][i]}

for oids in values['Unacceptable'][0]:
    if len(values['Unacceptable'][0]) == 1:
        new_dict[dirty_dictionary[key]['Unacceptable'][-1]].update({'Unacceptable': oids})
    if len(values['Unacceptable'][0]) > 1:
        Unacceptable = []
        for i in range(len(values['Unacceptable'][0])):
            for j in range(len(values['Unacceptable'][0][i])):
                Unacceptable.append(values['Unacceptable'][0][i][j])

        if len(values['Acceptable'][0]) > 1:
            for k in range(len(values['Acceptable'][0])):
                new_dict[dirty_dictionary[key]['Acceptable'][-1] + F'_{k}'].update({'Unacceptable': Unacceptable})
        else:
            new_dict[dirty_dictionary[key]['Unacceptable'][-1]].update({'Unacceptable': Unacceptable})

print(new_dict)

and the out put is :输出是:

{'DOG_GROUP': {'Acceptable': [35, 38], 'Unacceptable': [2, 29, 44, 50, 54, 60]}, 
'CAT_GROUP_0': {'Acceptable': [3, 6], 'Unacceptable': [12, 18, 45, 51, 61]}, 
'CAT_GROUP_1': {'Acceptable': [100, 101], 'Unacceptable': [12, 18, 45, 51, 61]}, 
'FISH_GROUP': {'Acceptable': [], 'Unacceptable': [13, 19, 22, 28, 34]}}

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

相关问题 如何从嵌套字典创建平面字典,其字符串是引用字典的子集? - How can I create a flat dictionary from a nested dictionary whose keys are a subset of a reference dictionary? 如何创建嵌套字典键并从命名空间键值对列表中为其分配值? - how can I create nested dictionary keys and assign them values from a list of namespaced key value pairs? 如何获取键值对以从嵌套字典创建字典? - How can I get the key value pair to create a dictionary from a nested dictionary? 如何从嵌套字典创建包含子字典键的列表? - How to create a List containing a sub dictionary keys , from nested dictionary? 如何从嵌套字典中获取键? - How can I get the keys from a nested dictionary? 如何从两个列表创建字典,一个列表是键,另一个包含嵌套值? - How can I create a dictionary from two lists, one list is the keys, the other contains nested values? 如何从嵌套字典创建新字典? - How to create new dictionaries from nested dictionary? 如何从python中的正则表达式创建嵌套字典? - How can I create a nested dictionary from a regex in python? 如何从现有列表创建嵌套字典? - How can I create a nested dictionary from existing lists? 如何确认一个值是否在嵌套字典中 - How can I confirm a value is in nested dictionary or not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM