You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效移除字典列表中ID存在于嵌套About中的字典?

高效移除字典列表中嵌套重复ID的条目

问题背景

我有一个规模极大的字典列表(简化示例如下):

list_of_dicts = [
    {'ID': 1234,
     'Name': 'Bobby',
     'Animal': 'Dog',
     'About': [{'ID': 5678, 'Food': 'Dog Food'}]},
    {'ID': 5678, 'Food': 'Dog Food'},
    {'ID': 91011,
     'Name': 'Jack',
     'Animal': 'Bird',
     'About': [{'ID': 1996, 'Food': 'Seeds'}]},
    {'ID': 1996, 'Food': 'Seeds'},
    {'ID': 2007,
     'Name': 'Bean',
     'Animal': 'Cat',
     'About': [{'ID': 2008, 'Food': 'Fish'}]},
    {'ID': 2008, 'Food': 'Fish'}
]

需求是移除所有ID与其他字典中About字段嵌套ID相同的条目(比如ID为2008的字典因为已嵌套在About中,需要被移除)。

我原本的代码在小示例中有效,但面对海量数据时,remove()无法一次性移除所有目标条目,需要多次运行:

nested_ids = [5678, 1996, 2008]
for i in list_of_dicts:
    if i['ID'] in nested_ids:
        list_of_dicts.remove(i)

期望输出:

[{'ID': 1234,
  'Name': 'Bobby',
  'Animal': 'Dog',
  'About': [{'ID': 5678, 'Food': 'Dog Food'}]},
 {'ID': 91011,
  'Name': 'Jack',
  'Animal': 'Bird',
  'About': [{'ID': 1996, 'Food': 'Seeds'}]},
 {'ID': 2007,
  'Name': 'Bean',
  'Animal': 'Cat',
  'About': [{'ID': 2008, 'Food': 'Fish'}]}]

问题根源

遍历列表的同时调用remove()会改变列表长度和元素索引,导致后续元素被跳过。比如移除第2个元素后,原第3个元素会移动到第2位,但循环索引已走到第3位,直接跳过该元素,因此需要多次运行才能清理干净。

优化方案

方案1:生成新列表(推荐,高效且安全)

直接过滤出不需要移除的元素生成新列表,时间复杂度O(n),适合海量数据,无索引问题:

首先自动收集所有嵌套ID(无需手动维护nested_ids,适配大规模数据):

# 收集所有About字段中的嵌套ID,用集合存储提升查询效率
nested_ids = set()
for item in list_of_dicts:
    if 'About' in item:
        for about_entry in item['About']:
            nested_ids.add(about_entry['ID'])

# 过滤掉ID在nested_ids中的条目
filtered_list = [item for item in list_of_dicts if item['ID'] not in nested_ids]

使用set存储嵌套ID是因为集合的in操作时间复杂度为O(1),远快于列表的O(n),处理海量数据时性能提升显著。

方案2:原地修改(倒序遍历)

若受内存限制无法生成新列表,可通过倒序遍历原地修改原列表,避免索引跳过问题:

# 先收集嵌套ID
nested_ids = set()
for item in list_of_dicts:
    if 'About' in item:
        for about_entry in item['About']:
            nested_ids.add(about_entry['ID'])

# 倒序遍历删除目标条目
for i in range(len(list_of_dicts)-1, -1, -1):
    if list_of_dicts[i]['ID'] in nested_ids:
        del list_of_dicts[i]

倒序遍历时,删除当前元素不会影响前面未遍历的元素索引,一次遍历即可完成清理。

方案对比

  • 方案1代码简洁易读,不修改原数据,避免意外副作用,且列表推导式经Python内部优化,执行效率更高,优先推荐。
  • 方案2适合内存紧张的场景,但原地修改可能引入潜在风险(如其他引用原列表的代码受影响)。

内容的提问来源于stack exchange,提问作者blah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 02:41:03