迭代嵌套字典的值

问题描述 投票:0回答:1
Nested_Dict =
    {'candela_samples_generic': {'drc_dtcs': {'domain_name': 'TEMPLATE-DOMAIN', 'dtc_all':{ 
    '0x930001': {'identification': {'udsDtcValue': '0x9300', 'FaultType': '0x11', 'description': 'GNSS antenna short to ground'}, 
    'functional_conditions': {'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground'}},
    '0x212021': {'identification': {'udsDtcValue': '0x2120', 'FaultType': '0x21', 'description': 'ECU internal Failure'},
    'functional_conditions': {'failure_name': 'short_to_ground', 'mnemonic': 'DTC_GNSS_Antenna_Short_to_ground'}}}}}}
   
 Header = {
        'dtc_all': {
            'DiagnosticTroubleCodeUds': {'udsDtcValue': None, 'FaultType': None},
            'dtcProps': {'description': None},
            'DiagnosticTroubleCodeObd': {'failure_name': None}   
        }
    }
    
SubkeyList = ['0x930001','0x212021']

Expected Output:    
New_Dict= 
{'dtc_all': 
{'0x930001': {'DiagnosticTroubleCodeUds': {'udsDtcValue': '0x9300', 'FaultType': '0x11'}, 'dtcProps':{'description': 'GNSS antenna short to ground'}, 'DiagnosticTroubleCodeObd': {'failure_name':short_to_ground}}},
{'0x212021': {'DiagnosticTroubleCodeUds': {'udsDtcValue': '0x2120', 'FaultType': '0x21'}, 'dtcProps':{'description': 'ECU internal Failure'}, 'DiagnosticTroubleCodeObd': {'failure_name':short_to_ground}}}}

参考问题:将输入聚合到统一字典中

这里想要在标头字典内迭代标头字典的值,但使用我的代码,它是迭代 dict 的键而不是 dict 的值。 从 SubkeyList 中获取一个元素,并从字典中一次获取一个标头键(可以有多个标头键,如 dtc_all)。在标头字典内迭代字典的值,例如“udsDtcValue”。

For example:
  Main_Key = dtc_all
  Sub_Key = 0x212021
  Element = udsDtcValue

将这些参数传递给函数 get_value_nested_dict(nested_dict, Main_Key, Sub_Key, Element)。该函数将返回元素值。 get_value_nested_dict 函数按预期工作,用于我发布的供参考的元素值检索。同时创建一个新的字典,并在正确的地方更新元素值,如'udsDtcValue': '0x9300'。另外,请确保密钥序列与标头中的序列保持相同。类似地,在标头字典内迭代字典的所有值,例如FaultType、description,直到failure_name。对 SubkeyList 中的每个元素重复这些迭代,并以相同的顺序将结果附加到 new_dict 中。关于如何继续的任何建议?

def create_new_dict(Nested_Dict, Header, SubkeyList):
    new_dict = {}
    for sub_key in SubkeyList:
        sub_dict = {}
        for element, value in Header['dtc_all'].items():
            value = get_value_nested_dict(Nested_Dict, 'dtc_all', sub_key, element)
            if value:
                sub_dict[element] = value[0]
        new_dict[sub_key] = sub_dict
    return new_dict
python dictionary dictionary-comprehension
1个回答
0
投票

好多了🤓

您需要确保对于

Header
字典结构的每个部分,我们不仅要迭代键,还要进入其嵌套结构以检索
udsDtcValue
FaultType
description
failure_name
来自
Nested_Dict

def get_value_from_nested_dict(nested_dict, path):
    for key in path:
        nested_dict = nested_dict.get(key, {})
        if not nested_dict:
            return None
    return nested_dict
def create_new_dict(nested_dict, header, subkey_list):
    new_dict = {'dtc_all': {}}
    path_mappings = {'DiagnosticTroubleCodeUds': ['identification'], 'dtcProps': ['identification'], 'DiagnosticTroubleCodeObd': ['functional_conditions']}
    for sub_key in subkey_list:
        sub_dict_structure = {}
        for header_key, inner_keys in header['dtc_all'].items():
            header_sub_dict = {}
            for inner_key in inner_keys.keys():
                base_path = ['candela_samples_generic', 'drc_dtcs', 'dtc_all', sub_key]
                specific_path = path_mappings.get(header_key, [])
                value_path = base_path + specific_path + [inner_key]
                value = get_value_from_nested_dict(nested_dict, value_path)
                if value is not None:
                    header_sub_dict[inner_key] = value
            if header_sub_dict:
                sub_dict_structure[header_key] = header_sub_dict
        if sub_dict_structure:
            new_dict['dtc_all'][sub_key] = sub_dict_structure
    return new_dict
        
© www.soinside.com 2019 - 2024. All rights reserved.