Home > Mobile >  Assign a value from dictionary to the list and convert the list to a dictionary
Assign a value from dictionary to the list and convert the list to a dictionary

Time:12-28

I have a dictionary and a list:

FuseValues = {'K0A': 0.11, 'K0B': 0.04, 'K0C': 0.07, 'K0D': 0.11, 'K1A': 0.09, 'K1B': 0.05, 'K1C': 0.03, 'K1D': 0.05, 'K2A': 0.05, 'K2B': 0.04, 'K2C': 0.05, 'K2D': 0.08, 'K3A': 0.08, 'K3B': 0.08, 'K3C': 0.07, 'K3D': 0.11}
Doc_A = [['207', '72', '232', '47', '220', '53', '87', '152', '215', '56', '99', '96', '65', '139', '5', '85', '127', '38', '129', '78', '51', '170', '28', '122', '71'], ['235', '91', '39', '124', '199', '248', '196', '175', '239', '125', '229', '164', '94', '22', '121', '75', '17', '193', '184', '179', '224', '158', '115', '79', '58'], ['41', '105', '80', '59', '173', '151', '42', '8', '128', '204', '21', '213', '16', '116', '246', '126', '195', '24', '25', '144', '178', '23', '155', '104', '10'], ['212', '223', '107', '243', '197', '225', '123', '167', '90', '69', '64', '20', '145', '200', '30', '222', '136', '43', '141', '57', '97', '77', '172', '82', '98']]

I want the output as a dictionary with the values of K0A assigned to first list, K1Ato the second, and so on in the list A, like below:

Output_Doc_A = {{'207':0.11, '72':0.11, '232':0.11, '47':0.11, '220':0.11, '53':0.11, '87':0.11, '152':0.11, '215':0.11, '56':0.11, '99':0.11, '96':0.11, '65':0.11, '139':0.11, '5':0.11, '85':0.11, '127':0.11, '38':0.11, '129':0.11, '78':0.11, '51':0.11, '170':0.11, '28':0.11, '122':0.11, '71':0.11},{'235':0.09, '91':0.09, '39':0.09, '124':0.09, '199':0.09, '248':0.09, '196':0.09, '175':0.09, '239':0.09, '125':0.09, '229':0.09, '164':0.09, '94':0.09, '22':0.09, '121':0.09, '75':0.09, '17':0.09, '193':0.09, '184':0.09, '179':0.09, '224':0.09, '158':0.09, '115':0.09, '79':0.09, '58':0.09}{...and so on}}

I tried a lot but couldn't figure out a possible solution to this. Can anyone please help?

CodePudding user response:

I believe this is what you are looking for (not tested), but the question was a little ambiguous so please forgive any errors, put it in the comments and I will amend the code.

A_keys = sorted([k for k in FuseValues.keys() if k[-1] == "A"])
Output_Doc_A = []
for i in range(len(A_keys))):
    Output_Doc_A.append(dict.fromkeys(Doc_A[i], FuseValues[A_keys[i]]))

CodePudding user response:

Here is a more verbose approach which hopefully helps you understand how this can be solved.

final = []

for num in range(len(Doc_A)):  # iterate over the index of Doc_A
    key = f"K{num}A"  # keys increment from K0A to KNA
    try:
        val = FuseValues[key]  # get the value we want for new keys
    except IndexError:  # in the chance the lengths don't match
        continue
    new_keys = Doc_A[num]  # grab the new keys by using index on Doc_A
    tmp = dict()  # create a temporary dictionary
    for nk in new_keys:  # for each of our new keys
        tmp[nk] = val  # set the value of that key in our temp dictionary
    final.append(tmp)  # append the temp dictionary to our final output list

We can also reduce the last for-loop to a dictionary comprehension as well.

for num in range(len(Doc_A)):
    key = f"K{num}A"
    try:
        val = FuseValues[key]
    except IndexError:
        continue
    new_keys = Doc_A[num]
    tmp = {nk: val for nk in new_keys}
    final.append(tmp)

CodePudding user response:

You can use a list comprehension with the fromkeys dictionary constructor and zip to pair up values with keys (assuming the keys are in the same order in the dictionary and the Doc_A list):

FuseValues = {'K0A': 0.11, 'K0B': 0.04, 'K0C': 0.07, 'K0D': 0.11, 'K1A': 0.09, 'K1B': 0.05, 'K1C': 0.03, 'K1D': 0.05, 'K2A': 0.05, 'K2B': 0.04, 'K2C': 0.05, 'K2D': 0.08, 'K3A': 0.08, 'K3B': 0.08, 'K3C': 0.07, 'K3D': 0.11}
Doc_A = [['207', '72', '232', '47', '220', '53', '87', '152', '215', '56', '99', '96', '65', '139', '5', '85', '127', '38', '129', '78', '51', '170', '28', '122', '71'], ['235', '91', '39', '124', '199', '248', '196', '175', '239', '125', '229', '164', '94', '22', '121', '75', '17', '193', '184', '179', '224', '158', '115', '79', '58'], ['41', '105', '80', '59', '173', '151', '42', '8', '128', '204', '21', '213', '16', '116', '246', '126', '195', '24', '25', '144', '178', '23', '155', '104', '10'], ['212', '223', '107', '243', '197', '225', '123', '167', '90', '69', '64', '20', '145', '200', '30', '222', '136', '43', '141', '57', '97', '77', '172', '82', '98']]

Values_A = (v for k,v in FuseValues.items() if k.endswith("A"))
Output_Doc_A = [ dict.fromkeys(a,v) for a,v in zip(Doc_A,Values_A)]
print(Output_Doc_A)
    
[{'207': 0.11, '72': 0.11, '232': 0.11, '47': 0.11, '220': 0.11, '53': 0.11, '87': 0.11, '152': 0.11, '215': 0.11, '56': 0.11, '99': 0.11, '96': 0.11, '65': 0.11, '139': 0.11, '5': 0.11, '85': 0.11, '127': 0.11, '38': 0.11, '129': 0.11, '78': 0.11, '51': 0.11, '170': 0.11, '28': 0.11, '122': 0.11, '71': 0.11}, {'235': 0.09, '91': 0.09, '39': 0.09, '124': 0.09, '199': 0.09, '248': 0.09, '196': 0.09, '175': 0.09, '239': 0.09, '125': 0.09, '229': 0.09, '164': 0.09, '94': 0.09, '22': 0.09, '121': 0.09, '75': 0.09, '17': 0.09, '193': 0.09, '184': 0.09, '179': 0.09, '224': 0.09, '158': 0.09, '115': 0.09, '79': 0.09, '58': 0.09}, {'41': 0.05, '105': 0.05, '80': 0.05, '59': 0.05, '173': 0.05, '151': 0.05, '42': 0.05, '8': 0.05, '128': 0.05, '204': 0.05, '21': 0.05, '213': 0.05, '16': 0.05, '116': 0.05, '246': 0.05, '126': 0.05, '195': 0.05, '24': 0.05, '25': 0.05, '144': 0.05, '178': 0.05, '23': 0.05, '155': 0.05, '104': 0.05, '10': 0.05}, {'212': 0.08, '223': 0.08, '107': 0.08, '243': 0.08, '197': 0.08, '225': 0.08, '123': 0.08, '167': 0.08, '90': 0.08, '69': 0.08, '64': 0.08, '20': 0.08, '145': 0.08, '200': 0.08, '30': 0.08, '222': 0.08, '136': 0.08, '43': 0.08, '141': 0.08, '57': 0.08, '97': 0.08, '77': 0.08, '172': 0.08, '82': 0.08, '98': 0.08}]

You could also do it with starmap (from itertools):

from itertools import starmap
Values_A     = (v for k,v in FuseValues.items() if k.endswith("A"))
Output_Doc_A = [*starmap(dict.fromkeys,zip(Doc_A,Values_A))]

If the keys are not in the same order but follow the pattern K#A where # is the index in Doc_A, then you can build the list of dictionaries like this:

[ dict.fromkeys(a,FuseValues[f'K{i}A']) for i,a in enumerate(Doc_A)]
  • Related