亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在具有一個鍵的多個值的字典中添加列表或字典 - 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 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號