将输入聚合到合并字典中

问题描述 投票: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','0x240001']

从 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 中。 关于如何继续的任何建议?

输出:New_Dict 应该如下所示:

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}}}}

def get_value_nested_dict(nested_dict, main_key, sub_key, element):
    results = []
    def search_for_element(sub_dict, looking_for_element):
        if isinstance(sub_dict, dict):
            for k, v in sub_dict.items():
                if k == element and looking_for_element:
                    results.append(v)
                else:
                    search_for_element(v, looking_for_element)
    def search_nested_dict(current_dict):
        if isinstance(current_dict, dict):
            for key, value in current_dict.items():
                if key == main_key:
                    if sub_key in value:
                        search_for_element(value[sub_key], True)
                    else:
                        search_for_element(value, False)
                else:
                    search_nested_dict(value)
    search_nested_dict(nested_dict)
    return results
python dictionary nested dictionary-comprehension
1个回答
0
投票
  1. 初始化一个空字典

    new_dict

  2. 迭代

    SubkeyList

  3. sub_key
    中的每个
    SubkeyList
    创建一个新字典:
    sub_dict

  4. 对于

    element
    中的每个
    Header['dtc_all']
    ,使用
    Nested_Dict
    get_value_nested_dict
    获取值并将其添加到
    sub_dict

  5. sub_dict
    添加到
    new_dict
     下的 
    sub_key

    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
    

    New_dict = create_new_dict(Nested_Dict, Header, SubkeyList)

© www.soinside.com 2019 - 2024. All rights reserved.