【问题标题】:How to append a value from a dictionary into a list of dictionaries under certain conditions如何在某些条件下将字典中的值附加到字典列表中
【发布时间】:2021-03-12 15:56:24
【问题描述】:

我有两个字典列表。我想追加到list_dicts1的嵌套字典中,当list_dicts2的文本中出现对应的名称时。例如。在list_dicts2 中,名称“Nat”出现在两个不同的文本中,所以我想将其附加到包含“Nat”的嵌套字典中 list_dicts1

问题:当名称出现在多个文本中时,我的代码会创建一个新的字典条目,而不是将其附加到同一个字典中。

list_dicts1= [{'id': '1', 'name': 'John', 'nested':[{'id': '1', 'text': 'text1'}]},
              {'id': '2', 'name': 'Nat', 'nested':[{'id': '2', 'text': 'text2'}]}]
list_dicts2=[{'id': 'A', 'text': 'this text contains the name John'},
             {'id': 'B', 'text': 'this text contains the name Nat'},
             {'id': 'C', 'text': 'this text also contains the name Nat'}]

期望的输出:

[{'id': '1',
  'name': 'John',
  'nested': [{'id': '1', 'text': 'text1'},
   {'id': 'A', 'text': 'this text contains the name John'}]},
 {'id': '2',
  'name': 'Nat',
  'nested': [{'id': '2', 'text': 'text2'},
   {'id': 'B', 'text': 'this text contains the name Nat'},
   {'id': 'C', 'text': 'this text also contains the name Nat'}]}]

我的代码:

  for d in list_dicts1:
        for dic in list_dicts2: 
            if d.get('name', '') in dic.get('text', ''):
                d["nested"].append(dic)
                print(d)

当前输出:

[{'id': '1',
  'name': 'John',
  'nested': [{'id': '1', 'text': 'text1'},
   {'id': 'A', 'text': 'this text contains the name John'}]},
 {'id': '2',
  'name': 'Nat',
  'nested': [{'id': '2', 'text': 'text2'},
   {'id': 'B', 'text': 'this text contains the name Nat'}]},
 {'id': '2',
  'name': 'Nat',
  'nested': [{'id': '2', 'text': 'text2'},
   {'id': 'B', 'text': 'this text contains the name Nat'},
   {'id': 'C', 'text': 'this text also contains the name Nat'}]}]

正如您所见,在当前输出中创建了一个新的“Nat”条目,这不是我们的想法。
我希望我的问题足够清楚(通过将这些词典粘贴到您的编辑器中最容易重现)。在发布问题之前,我在代码中重现了这个问题。

【问题讨论】:

  • 当我尝试运行你的代码时,我得到了正确的输出。
  • print 语句的当前“输出”吗?
  • 打印语句在每次迭代中执行。你不应该在最后打印一次吗?
  • 将打印语句移到“for d in list_dicts1”块之外
  • 所以你的答案给出了这个问题的正确输出 - 但不幸的是它不是我要找的,因为“list_dicts1”实际上是一个 solr 数据库对象(在我的代码中),但我试图在这里重现它带有虚拟数据。我要解决的问题最后没有写在我自己的问题中,但是很难在 stackoverflow 上重现 - 但我会接受你的回答,谢谢。

标签: python list dictionary for-loop nested


【解决方案1】:

查看键 'name' 的值是否在键 'text' 的值中,如果是,则将整个字典添加到键 'nested'

import pprint


list_dicts1= [{'id': '1', 'name': 'John', 'nested':[{'id': '1', 'text': 'text1'}]},
              {'id': '2', 'name': 'Nat', 'nested':[{'id': '2', 'text': 'text2'}]}]

list_dicts2=[{'id': 'A', 'text': 'this text contains the name John'},
             {'id': 'B', 'text': 'this text contains the name Nat'},
             {'id': 'C', 'text': 'this text also contains the name Nat'}]

for dictionary in list_dicts1:
  for b_dict in list_dicts2:
    if dictionary['name'] in b_dict['text']:
      dictionary['nested'] = [dictionary['nested'][0],b_dict]

pprint.pprint(list_dicts1)

>>> [{'id': '1',
  'name': 'John',
  'nested': [{'id': '1', 'text': 'text1'},
             {'id': 'A', 'text': 'this text contains the name John'}]},
 {'id': '2',
  'name': 'Nat',
  'nested': [{'id': '2', 'text': 'text2'},
             {'id': 'C', 'text': 'this text also contains the name Nat'}]}]

这一行

[dictionary['nested'][0],b_dict]

返回键 'nested' 的列表内的字典,因为此值最初是一个列表,其中包含一个字典,而 b_dict 只是一个字典。

【讨论】:

    【解决方案2】:

    如果将 print 语句移到 for 循环之外,您应该会得到正确的输出。

    list_dicts1= [{'id': '1', 'name': 'John', 'nested':[{'id': '1', 'text': 'text1'}]},
                  {'id': '2', 'name': 'Nat', 'nested':[{'id': '2', 'text': 'text2'}]}]
    
    list_dicts2=[{'id': 'A', 'text': 'this text contains the name John'},
                 {'id': 'B', 'text': 'this text contains the name Nat'},
                 {'id': 'C', 'text': 'this text also contains the name Nat'}]
    
    for d in list_dicts1:
        for dic in list_dicts2: 
            if d.get('name', '') in dic.get('text', ''):
                d["nested"].append(dic)
                
    print(list_dicts1) # Prints the desired output
    

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 2020-12-06
      • 1970-01-01
      • 2023-03-23
      • 2021-07-02
      • 2023-04-04
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多