Very first Post here, apologies if it is not proper. I have 2 Dictionaries with keys and lists as values. I need to assign a list to an element of the list in a dictionary where it matches in other Dictionary 2.
Dictionary 1
{'S': ['Close Coupled', 'Wall Hung', 'Btw'], 'E':['Bifold', 'Hinge', 'Sliding', 'Pivot']}
Dictionary 2
{'Close Coupled': ['Close Coupled Contract', 'Close Coupled Open Back', 'Close Coupled Open Back Rimless'], 'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless'],'Btw': ['BTW Contract', 'BTW Rimless'], 'Bifold': ['700', '800', '900', '1000'], 'Hinge': ['700', '800', '900', '1000'], 'Sliding': ['700', '800', '900', '1000'], 'Pivot': ['700', '800', '900', '1000']}
Result I am trying to get is.
{'S': {'Close Coupled':['Close Coupled Contract', 'Close Coupled Open Back', 'Close Coupled Open Back Rimless'], 'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless'], 'Btw': ['BTW Contract', 'BTW Rimless'], 'E': 'Bifold':['700', '800', '900', '1000'], 'Hinge':['700', '800', '900', '1000'],'Sliding':['700', '800', '900', '1000'], 'Pivot':['700', '800', '900', '1000']}
After his I have another dictionary which will be added same way. it is like a Tree structure or nested but I am unable to build up my logic to assign dictionary to each matching element of the list in 1st dictionary.
If it is not clear; please let me know I will try to explain it better.
Thank you
You can use dict
comprehension to do this:
{keys : {m : d2.get(m) for m in values} for keys, values in d1.items()}
{'S': {'Close Coupled': ['Close Coupled Contract',
'Close Coupled Open Back',
'Close Coupled Open Back Rimless'],
'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless'],
'Btw': ['BTW Contract', 'BTW Rimless']},
'E': {'Bifold': ['700', '800', '900', '1000'],
'Hinge': ['700', '800', '900', '1000'],
'Sliding': ['700', '800', '900', '1000'],
'Pivot': ['700', '800', '900', '1000']}}
DATA:
d1 = {'S': ['Close Coupled', 'Wall Hung', 'Btw'], 'E':['Bifold', 'Hinge', 'Sliding', 'Pivot']}
d2 = {'Close Coupled': ['Close Coupled Contract', 'Close Coupled Open Back', 'Close Coupled Open Back Rimless'], 'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless'],'Btw': ['BTW Contract', 'BTW Rimless'], 'Bifold': ['700', '800', '900', '1000'], 'Hinge': ['700', '800', '900', '1000'], 'Sliding': ['700', '800', '900', '1000'], 'Pivot': ['700', '800', '900', '1000']}
I don't think I have understood your question correctly. However check this code and if it doesn't suit your need kindly let me know.
d1= {'S': ['Close Coupled', 'Wall Hung', 'Btw'], 'E':['Bifold', 'Hinge', 'Sliding', 'Pivot']}
d2= {'Close Coupled': ['Close Coupled Contract', 'Close Coupled Open Back', 'Close Coupled Open Back Rimless'], 'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless'],'Btw': ['BTW Contract', 'BTW Rimless'], 'Bifold': ['700', '800', '900', '1000'], 'Hinge': ['700', '800', '900', '1000'], 'Sliding': ['700', '800', '900', '1000'], 'Pivot': ['700', '800', '900', '1000']}
final_dict= {} # create a dictionary to store the final answer
for item in d1:
temp= dict() # temporary dictionary
for i in item d1[item]:
temp[i]= d2[i]
final_dict[item]= temp
output
print(final_dict)
{'E': {'Bifold': ['700', '800', '900', '1000'],
'Hinge': ['700', '800', '900', '1000'],
'Pivot': ['700', '800', '900', '1000'],
'Sliding': ['700', '800', '900', '1000']},
'S': {'Btw': ['BTW Contract', 'BTW Rimless'],
'Close Coupled': ['Close Coupled Contract',
'Close Coupled Open Back',
'Close Coupled Open Back Rimless'],
'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless']}} `
One approach would be to loop through your first dictionary and get the lists that correspond to the keys of the same name from your second dictionary. For example:
d1 = {'S': ['Close Coupled', 'Wall Hung', 'Btw'], 'E':['Bifold', 'Hinge', 'Sliding', 'Pivot']}
d2 = {'Close Coupled': ['Close Coupled Contract', 'Close Coupled Open Back', 'Close Coupled Open Back Rimless'], 'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless'],'Btw': ['BTW Contract', 'BTW Rimless'], 'Bifold': ['700', '800', '900', '1000'], 'Hinge': ['700', '800', '900', '1000'], 'Sliding': ['700', '800', '900', '1000'], 'Pivot': ['700', '800', '900', '1000']}
result = dict()
for key, values in d1.items():
result[key] = dict()
for value in values:
result[key][value] = d2[value]
print(result)
# OUTPUT (print does not output indented results shown here for readability only)
# {
# 'S': {
# 'Close Coupled': ['Close Coupled Contract', 'Close Coupled Open Back', 'Close Coupled Open Back Rimless'],
# 'Btw': ['BTW Contract', 'BTW Rimless'],
# 'Wall Hung': ['Wall Hung Contract', 'Wall Hung Rimless']
# },
# 'E': {
# 'Bifold': ['700', '800', '900', '1000'],
# 'Hinge': ['700', '800', '900', '1000'],
# 'Sliding': ['700', '800', '900', '1000'],
# 'Pivot': ['700', '800', '900', '1000']
# }
# }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.