根据键字典从元组列表中检索元素的值并创建新的格式化字典

问题描述 投票:0回答:1
List_of_tuples=
 [('SWC', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'functional_conditions', 'events', 0, 'eventKind')),
 ('Throttle/Pedal Position Sensor/Switch "D"-General Electrical Failure', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'functional_conditions', 'events', 0, 'longName')),
  ('0x2120', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'identification', 'udsDtcValue')),
 ('0x21', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'identification', 'fault_type')),
  (40, ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'functional_conditions', 'obd', 'aging', 'aging.threshold')),
 ('WUC', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'functional_conditions', 'obd', 'aging', 'aging_cycle')),
  ('MIL', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'connected_indicator', 'indicator', 0)),
 ('G1', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'connected_indicator', 'indicator', 1)),
 ('BLINK-MODE', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'connected_indicator', 'behavior', 0)),
 ('CONTINUOUS-MODE-ON', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'connected_indicator', 'behavior', 1)),
  ('Custom', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'functional_conditions', 'events', 0, 'debounceAlgorithm')),
 ('Enable_1', ('candela_samples_generic', 'drc_dtcs', 'dtc_all', '0x212021', 'functional_conditions', 'events', 0, 'enableCondition'))]

Keydict = {
    'dtc_all': {
        'DiagnosticEvent': {'eventKind': None, 'longName': None},
        'DiagnosticTroubleCodeUds': {'udsDtcValue': None, 'fault_type': None},
        'dtcProps': {'aging.threshold': None, 'aging_cycle': None},
        'connected_indicator': {'behavior': None, indicator:None},
        'DiagnosticEventToDebounceAlgorithmMapping': {'debounceAlgorithm': None},
    }
}

SubkeyList = ['0x240001']

迭代 Keydict 中的键和 subkeylist 中的子键。从 Keydict 获取元素(例如“eventKind”、“longName”)。然后,迭代元组列表,并对于每个元组,搜索与提取的键-子键-元素(例如 dtc_all、0x212021、eventKind)组合的匹配项。如果找到匹配项,则按照以下格式将外部元组元素附加到新的嵌套字典中。 注意:SubkeyList 中可以有多个元素,我们还需要迭代 SubkeyList 并将其以相同的格式附加到 new_dict 中。

New_Dict= 
{'dtc_all': {'0x212021':
{'DiagnosticEvent': {'eventKind': 'SWC', 'longName': 'Throttle/Pedal Position Sensor/Switch "D"-General Electrical Failure'}, {'DiagnosticTroubleCodeUds': {'udsDtcValue': '0x2120', 'fault_type': '0x21'}, 
'dtcProps':{'aging.threshold': '40','aging_cycle': 'WUC'}, 
'connected_indicator': {'behavior': 'BLINK-MODE,CONTINUOUS-MODE-ON', indicator:'MIL,G1'},
'DiagnosticEventToDebounceAlgorithmMapping': {'debounceAlgorithm':Custom}}}}}
python dictionary nested
1个回答
0
投票

又来了🤓

New_Dict = {k: {sk: {} for sk in SubkeyList} for k in Keydict}
for outer_element, details in List_of_tuples:
    key = details[2]
    subkey = details[3]
    path = details[4:-1] 
    element_key = details[-1]
    if key in New_Dict and subkey in New_Dict[key]:
        current_dict = New_Dict[key][subkey]
        for p in path:
            if p not in current_dict:
                current_dict[p] = {}
            current_dict = current_dict[p]
        if element_key in current_dict:
            if current_dict[element_key] is not None:
                current_dict[element_key] += ',' + outer_element
            else:
                current_dict[element_key] = outer_element
        else:
            current_dict[element_key] = outer_element
def clean_empty(d):
    if not isinstance(d, dict):
        return d
    else:
        cleaned = {k: clean_empty(v) for k, v in d.items() if v and clean_empty(v)}
        return cleaned if cleaned else None
New_Dict = clean_empty(New_Dict)
New_Dict
© www.soinside.com 2019 - 2024. All rights reserved.