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

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

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']
# }
# }
添加回答
舉報