在字典中为一个键添加多个值的列表或字典

2024-09-28 05:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

第一次在这里发帖,如果不合适,请道歉。 我有两个字典,键和列表作为值。我需要给字典中的列表元素分配一个列表,它与另一个字典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']}

在他的字典之后,我有另一本字典,它将以同样的方式添加。它就像一个树结构或嵌套的,但我无法建立我的逻辑分配字典到每个匹配的第一字典列表元素。你知道吗

如果不清楚,请告诉我,我会尽量解释清楚的。你知道吗

谢谢


Tags: 元素列表close字典backopencontractwall
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']
#          }
# }

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

我想我没有正确理解你的问题。不过,请检查这个代码,如果它不适合你的需要,请让我知道。你知道吗

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

输出
打印(最终记录)

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

相关问题 更多 >

    热门问题