Search code examples
pythondictionarynested-loops

Nested Dictionary Pair Generation


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, 'sert'))

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


Solution

  • 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')
    }