为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

素胚勾勒不出你 2021-06-04 17:31:01
第一次在此发帖,如有不对之处请见谅。我有 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']}在他之后,我有另一本将以相同方式添加的字典。它就像一个树结构或嵌套,但我无法建立我的逻辑来将字典分配给第一个字典中列表的每个匹配元素。如果不清楚;请让我知道我会尝试更好地解释它。
查看完整描述

3 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

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


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


查看完整回答
反对 回复 2021-06-09
?
智慧大石

TA贡献1946条经验 获得超3个赞

您可以使用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']}


查看完整回答
反对 回复 2021-06-09
?
慕容3067478

TA贡献1773条经验 获得超3个赞

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


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

#          }

# }


查看完整回答
反对 回复 2021-06-09
  • 3 回答
  • 0 关注
  • 188 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信