May-27-2024, 10:47 AM
(May-26-2024, 08:27 PM)DeaD_EyE Wrote: My try...
dict1 = { "SAG01112_SSAP_HA_LPM": [["OS_TYPE", "AIX"], ["IS_COBOL", "1"]], "SAP": [], "C11_RG": [], "W11_RG": [], } dict2 = { "SAG01112_SSAP_HA_LPM": [ ["OS_TYPE", "AIX"], ["IP", "172.17.10.112"], ["IP", "10.111.160.119"], ["IP", "10.111.160.68"], ["IP", "10.111.160.66"], ["IP", "10.95.0.112"], ["IP", "10.111.162.119"], ], "SAP": [], "C11_RG": [], "W11_RG": [], } from itertools import chain def deduplicate(*dicts): """ Generator: Deduplicate all iterable values for each key for each dict. The order is kept by the occourence of keys in the first dict, second dict, ... """ all_keys = [] # iterate over *dicts and append them only, if they don't exist # this keeps the key-order of the first dict, second dict, ... for key in chain.from_iterable(dicts): if key not in all_keys: all_keys.append(key) print("all_keys:", all_keys) # iterate over all keys for key in all_keys: # list of results, this will later yielded result = [] # iterate over dicts for input_dict in dicts: # for each dict, call get(key, []) which reuturns an emtpy list # if the key does not exist, otherwise the value is returned for value in input_dict.get(key, []): if value in result: print("Duplicate:", value) # skipping the value if it's already in result continue # append, if the value is not in result result.append(value) yield key, result # you could also use result.clear(), # but then you have to yield a copy with result.copy() # here a new list is assigned, and no copy does happen result = [] def deduplicate2dict(*dicts): """ Wrapper function to return a dict """ return dict(deduplicate(*dicts)) result1 = dict(deduplicate(dict1, dict2)) result2 = deduplicate2dict(dict1, dict2)
shows weird behaviour...data is added to the delete data dict instead of removal...
root@ssap: /tmp # ./aix_reg_client_tcp_dev.py --run-now changed: {'SAG01112_SSAP_HA_LPM': [['OS_TYPE', 'AIXi']]} ONLY ECHO, NOTHING SENT TO SERVER DEBUG ON: STD|1|SAG01112_SSAP_HA_LPM|OS_TYPE|AIXi|63b560175e8a15e180b58498f6910fb2 deleted: {'SAG01112_SSAP_HA_LPM': [['OS_TYPE', 'AIX']], 'SAP': [], 'C11_RG': [], 'W11_RG': []} all_keys: ['SAG01112_SSAP_HA_LPM', 'SAP', 'C11_RG', 'W11_RG'] del filtered {'SAG01112_SSAP_HA_LPM': [['OS_TYPE', 'AIX'], ['OS_TYPE', 'AIXi']], 'SAP': [], 'C11_RG': [], 'W11_RG': []} ONLY ECHO, NOTHING SENT TO SERVER DEBUG ON: DEL|1|SAG01112_SSAP_HA_LPM|OS_TYPE|AIX|63b560175e8a15e180b58498f6910fb2 DEBUG ON: DEL|1|SAG01112_SSAP_HA_LPM|OS_TYPE|AIXi|63b560175e8a15e180b58498f6910fb2