请先在这里发帖,如果这不恰当,请表示歉意。我有两个字典,键和列表作为值。我需要将一个列表分配给字典中的一个元素,在其中它与其他字典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']}在他的后面,我有另一本字典,它将以同样的方式加起来。它就像一个树形结构或嵌套结构,但是我无法建立我的逻辑来将字典分配给第一个字典中列表中的每个匹配元素。
如果不清楚,请告诉我,我会尽量更好地解释。
谢谢
发布于 2018-09-04 06:05:27
我想我没有正确理解你的问题。但是,请检查这段代码,如果它不适合您的需要,请告诉我。
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']}} `发布于 2018-09-04 03:53:41
您可以使用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']}发布于 2018-09-04 03:28:59
一种方法是遍历第一个字典,并从第二个字典中获得与同名键对应的列表。例如:
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']
# }
# }https://stackoverflow.com/questions/52157879
复制相似问题