首先在这里发布,如果不合适的话,我们深表歉意。我有2个字典,其中包含键和列表作为值。我需要在字典中与其他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']}
在他之后,我还有另一本字典,将以同样的方式添加。它就像一个树形结构或嵌套结构,但是我无法建立逻辑将字典分配给第一字典中列表的每个匹配元素。
如果不清楚;请让我知道,我会尽力向您解释。
谢谢
我认为我没有正确理解您的问题。但是,请检查此代码,如果它不适合您的需求,请告诉我。
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
输出
打印(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']}} `
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句