2
votes

I have an input dictionary:

dict1 = {'AM': ['tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR', 
      'tr','fs','nz','tz','dz'],
      'BR': ['tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR', 
      'tr''fs','nz','tz','dz'],
      'ZR':'[tv', 'rs', 'pq', 'MN', 'tN', 'tq', 'OP', 'tP', 'QR', 
      'tr','fs','nz','tz','dz']}

Two other dictionaries in which there is a need to search:

dict2 = {'AM':{'pq':1.2,'rs':2.41,'tv':3.81},'BR':{'MN':1.3,'OP':1.41,'QR':1.81},'ZR': 
      {'fs':1.2,'nz':1.5,'tz':1.7,'dz':1.3}}
dict3 = {'AM':{'tq':1.3,'rs':1.41,'tv':2.41},'BR':{'tN':1.8,'tP':1.81,'tr':1.42}}

Desired Output

{'AM-tv': (3.81,2.41),
 'AM-rs': (2.41,1.41),
 'AM-pq': (1.2,'sert'),
 'AM-MN': ('sert','sert'),
 'AM-tN': ('sert','sert'),
 'AM-tq': ('sert',1.3),
 'AM-OP': ('sert','sert'),
 'AM-tP': ('sert','sert'),
 'AM-QR':- ('sert','sert'),
 'AM-tR':- ('sert','sert'),
 'AM-fs':- ('sert','sert'),
 'AM-nz':- ('sert','sert'),
 'AM-tz':- ('sert','sert'),
 'AM-dz': ('sert','sert'),
 'ZR-tv': ('sert','sert'),
 'ZR-rs': ('sert','sert'),
 'ZR-pq': ('sert','sert'),
 'ZR-MN': ('sert','sert'),
 'ZR-tN': ('sert','sert'),
 'ZR-tq': ('sert','sert'),
 'ZR-OP': ('sert','sert'),
 'ZR-tP': ('sert','sert'),
 'ZR-QR': ('sert','sert'),
 'ZR-tr': ('sert','sert'),
 'ZR-fs': (1.2,'sert'),
 'ZR-nz': (1.5,'sert'),
 'ZR-tz':(1.7,'sert'),
 'ZR-dz': (1.3,'sert')
 'BR-tv': ('sert','sert'),
 'BR-rs': ('sert','sert'),
 'BR-pq': ('sert','sert'),
 'BR-MN': (1.3,'sert'),
 'BR-tN': ('sert',1.8),
 'BR-tq': ('sert','sert'),
 'BR-OP': (1.41,'sert'),
 'BR-tP': ('sert',1.81),
 'BR-QR': (1.81,'sert'),
 'BR-tr': ('sert',1.42),
 'BR-fs':- ('sert','sert'),
 'BR-nz':- ('sert','sert'),
 'BR-tz':- ('sert','sert'),
 'BR-dz': ('sert','sert')}

In the output, there is a need to generate pairs of dict1. if the values of dict1 are present within the inner nested dictionary keys of dict2 or dict3 then it will replace with the inner nested dictionary values otherwise it will replace it with 'sert' string. The outer dictionary keys within dict2 and dict3 are not same. Is there any way to do it? The code which I have tried is this one:

out = {}
for k, lst in dict1.items():
    for v in lst:
       out[f"{k}-{v}"] = (dict2[k].get(v, 'sert'), 
dict3[k].get(v, 'insert'))

But this doesn't work, however my outer dictionary keys are different.

3
Please update your question with the code you have tried.quamrana

3 Answers

2
votes

Flatten the two dictionaries containing floating point numbers as values. Then, generate all of the keys in the result using a list comprehension. Finally, build the result:

flat_dict2 = {f"{k}-{inner_k}": v for k in dict2 for inner_k, v in dict2[k].items()}
flat_dict3 = {f"{k}-{inner_k}": v for k in dict3 for inner_k, v in dict3[k].items()}
result_keys = [f"{k}-{inner_k}" for k in dict1 for inner_k in dict1[k]]

{key: (flat_dict2.get(key, "sert"), flat_dict3.get(key, "sert")) for key in result_keys}

This outputs (only writing out the first three and last three key-value pairs since the output is pretty long):

{
 'AM-tv': (3.81, 2.41), 'AM-rs': (2.41, 1.41), 'AM-pq': (1.2, 'sert'),
 ...
 'ZR-nz': (1.5, 'sert'), 'ZR-tz': (1.7, 'sert'), 'ZR-dz': (1.3, 'sert')
}
1
votes

You can do it like this (though the results aren't in the same order as desired but that doesn't really matter in a dictionary):

result = {}
for i, j in dict1.items():
    for k in j:
        result[f'{i}-{k}'] = []
        for m in dict2, dict3:
            try:
                result[f'{i}-{k}'].append(m[i][k])
            except KeyError:
                result[f'{i}-{k}'].append('sert')
1
votes

Try without preprocessing like below, you need two dict.get() and search base key from dict1 and vals from value_list of dict1

res = {}
for k1,v1 in dict1.items():
    for val in v1:
        res[f'{k1}_{val}'] = (
            dict2.get(k1,{}).get(val, 'sert'), 
            dict3.get(k1,{}).get(val, 'sert')
            )
print(res)

Or as a one-line:

res = {f'{k1}_{val}' : (dict2.get(k1,{}).get(val, 'sert'), dict3.get(k1,{}).get(val, 'sert')) for k1,v1 in dict1.items() for val in v1}

Output:

{'AM_tv': (3.81, 2.41), 'AM_rs': (2.41, 1.41), 'AM_pq': (1.2, 'sert'), 
 'AM_MN': ('sert', 'sert'), 'AM_tN': ('sert', 'sert'), 'AM_tq': ('sert', 1.3), 
 'AM_OP': ('sert', 'sert'), 'AM_tP': ('sert', 'sert'), 'AM_QR': ('sert', 'sert'), 
 'AM_tr': ('sert', 'sert'), 'AM_fs': ('sert', 'sert'), 'AM_nz': ('sert', 'sert'), 
 'AM_tz': ('sert', 'sert'), 'AM_dz': ('sert', 'sert'), 'BR_tv': ('sert', 'sert'), 
 'BR_rs': ('sert', 'sert'), 'BR_pq': ('sert', 'sert'), 'BR_MN': (1.3, 'sert'), 
 'BR_tN': ('sert', 1.8), 'BR_tq': ('sert', 'sert'), 'BR_OP': (1.41, 'sert'), 
 'BR_tP': ('sert', 1.81), 'BR_QR': (1.81, 'sert'), 'BR_trfs': ('sert', 'sert'), 
 'BR_nz': ('sert', 'sert'), 'BR_tz': ('sert', 'sert'), 'BR_dz': ('sert', 'sert'), 
 'ZR_tv': ('sert', 'sert'), 'ZR_rs': ('sert', 'sert'), 'ZR_pq': ('sert', 'sert'), 
 'ZR_MN': ('sert', 'sert'), 'ZR_tN': ('sert', 'sert'), 'ZR_tq': ('sert', 'sert'), 
 'ZR_OP': ('sert', 'sert'), 'ZR_tP': ('sert', 'sert'), 'ZR_QR': ('sert', 'sert'), 
 'ZR_tr': ('sert', 'sert'), 'ZR_fs': (1.2, 'sert'), 'ZR_nz': (1.5, 'sert'), 
 'ZR_tz': (1.7, 'sert'), 'ZR_dz': (1.3, 'sert')}