【问题标题】:Adding list or dictionary in a dictionary with multiple values for one key - Python在一个键的多个值的字典中添加列表或字典 - Python
【发布时间】:2019-02-08 23:46:31
【问题描述】:

第一次在这里发帖,如有不妥请见谅。 我有 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']}

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

如果不清楚;请让我知道我会尝试更好地解释它。

谢谢

【问题讨论】:

  • 我试图解决你的问题,但是我不相信你写的预期答案。根据您的说法,您的输出必须是只有一个项目的字典,其中键是“S”,值是字典。能否请您检查一下您的问题并回复我一些澄清?

标签: python python-3.x dictionary tree nested


【解决方案1】:

我认为我没有正确理解您的问题。但是请检查此代码,如果它不适合您的需要,请告诉我。

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

【讨论】:

  • 谢谢。这和我想做的一样。但是代码中存在语法错误“for i in [---item----] d1[item]:”我昨天也在做同样的事情,但不知道为什么我会收到列表的哈希错误。
  • @Usman Mughal 我已经修改了代码,现在可以正常工作了。如果代码存在任何问题,请告诉我。
【解决方案2】:

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

【讨论】:

    【解决方案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']
    #          }
    # }
    

    【讨论】:

    • 谢谢。这也很好用..但是将字典转换为列表。所以不确定我是否能够对其进行进一步的索引,因为我需要进一步索引。最终结果应该类似于 S010101 或 E010201... E': [{'Bifold': ['700', '800', '900', '1000']},{'Hinge': ['700', '800', '900', '1000']}] E [{01:[ 01,02,03,04,05]},{ 02:[01,02,03,04,05]} 。 ...等等……
    • @UsmanMughal 对此感到抱歉。编辑了正确输出的答案。
    • 也就是说,@Onyambu 的回答比这个要优雅一点
    • @benvc - 可能是什么列表没有匹配的键?
    • @UsmanMughal 在当前答案中,如果d1 中有一个列表项,而d2 中没有相应的键,则以该项目为键和一个空列表作为值的字典将是添加到result。如果d2 中有一个键,而d1 中没有对应的列表项,那么它将被忽略。如果您尝试考虑特定场景,您可以相应地编辑您的问题(包括数据集中的示例)或发布另一个问题,以便我们提供帮助。
    猜你喜欢
    • 2022-06-23
    • 2023-01-12
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多