在字典中为一个键添加具有多个值的列表或字典-Python

时间:2018-09-04 02:05:25

标签: python python-3.x dictionary tree nested

请先在这里发帖,如果不合适的话,我们深表歉意。 我有2个字典,其中包含键和列表作为值。我需要在字典中与其他字典2中匹配的字典中的列表元素中分配一个列表。

Export-Csv

在他之后,我还有另一本字典,将以同样的方式添加。它就像一个树形结构或嵌套结构,但是我无法建立逻辑将字典分配给第一个字典中列表的每个匹配元素。

如果不清楚;请让我知道,我会尽力向您解释。

谢谢

3 个答案:

答案 0 :(得分:1)

您可以使用dict理解来做到这一点:

{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']}}

数据:

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']}

答案 1 :(得分:1)

我认为我没有正确理解您的问题。但是,请检查此代码,如果它不适合您的需要,请告诉我。

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']}} `

答案 2 :(得分:0)

一种方法是遍历您的第一本词典,并从第二本词典中获取与同名键相对应的列表。例如:

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']
#          }
# }