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