【发布时间】:2019-02-08 23:46:31
【问题描述】:
第一次在这里发帖,如有不妥请见谅。 我有 2 个字典,其中键和列表作为值。我需要将列表分配给字典中与其他字典 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']}
在他之后,我有另一本字典,将以同样的方式添加。它就像一个树结构或嵌套,但我无法建立我的逻辑来将字典分配给第一个字典中列表的每个匹配元素。
如果不清楚;请让我知道我会尝试更好地解释它。
谢谢
【问题讨论】:
-
我试图解决你的问题,但是我不相信你写的预期答案。根据您的说法,您的输出必须是只有一个项目的字典,其中键是“S”,值是字典。能否请您检查一下您的问题并回复我一些澄清?
标签: python python-3.x dictionary tree nested