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

Python中移除既是外层字典键又是嵌套值的重复项

高效清理字典冗余键的实现方法

问题背景

给定如下Python字典:

x = {'A fantastic gift for art lovers': ['Designed for adults, this stunning piece '
                                     'of 3D art can be proudly displayed on a '
                                     'wall following a rewarding build '
                                     'experience.'],
 'Build and relax': ['Art lovers can enjoy a relaxing and immersive building '
                     'experience as they create this unique artwork from 1,810 '
                     'pieces.'],
 'Create your own artwork': ['Build Hokusai’s The Great Wave with layers of '
                             'LEGO® bricks.'],
 'Finishing touch': ['Add a decorative tile with Hokusai’s signature.'],
 'Hokusai – The Great Wave': ['Create your own artwork',
                              'Build Hokusai’s The Great Wave with layers of '
                              'LEGO® bricks.',
                              'The Great Wave comes to life!',
                              'The picture’s multiple layers create a stunning '
                              '3D effect.',
                              'Finishing touch',
                              'Add a decorative tile with Hokusai’s '
                              'signature.'],
 'Immerse yourself in the world of art': ['Listen to the set’s Soundtrack, '
                                          'tailor-made with content to enhance '
                                          'the time you spend building this '
                                          'Japanese wall art.'],
 'Recreate an iconic piece of Japanese art': ['Celebrate your passion for '
                                              'Japanese art when you build '
                                              'this incredible LEGO® version '
                                              'of Hokusai’s The Great Wave.'],
 'The Great Wave comes to life!': ['The picture’s multiple layers create a '
                                   'stunning 3D effect.']}

这个字典存在冗余内容:有些内层列表里的元素本身是字典的键,且这些键对应的内容已经被包含在其他键的列表中——比如键'The Great Wave comes to life!'对应的内容,已经完整出现在'Hokusai – The Great Wave'的列表当中。我们需要删掉这些冗余键,只保留有效内容。

之前尝试的两种实现方案都有效率瓶颈:

  • 方案一:需要两次遍历字典,无法适配千万/亿级的大规模字典
  • 方案二:动态检查键是否存在,逻辑冗余,性能不佳

高效实现思路

你提到的扁平化所有值后与键取交集的思路,确实能大幅提升大字典场景下的处理速度,核心逻辑如下:

  1. 先将字典所有内层列表的内容扁平化,转为集合(集合的查找操作是O(1),远快于列表)
  2. 筛选出既是字典键、又出现在扁平化值集合中的字符串——这些就是需要删除的冗余键
  3. 从原字典中剔除这些冗余键,得到清理后的字典

这种方法的优势很明确:

  • 用集合操作替代多次遍历检查,时间复杂度降至O(N)(N为字典所有元素的总数)
  • 仅需两次遍历(一次扁平化值,一次过滤键),完全适配超大规模字典的处理需求

代码实现

# 1. 扁平化所有值并转为集合,自动去重
all_values = set()
for vals in x.values():
    all_values.update(vals)

# 2. 筛选出需要删除的冗余键:既是字典键,又出现在值集合中
keys_to_remove = [key for key in x if key in all_values]

# 3. 生成清理后的字典(避免直接修改原字典引发异常)
cleaned_dict = {key: val for key, val in x.items() if key not in keys_to_remove}

验证结果

清理后的字典会移除'Create your own artwork'、'Finishing touch'、'The Great Wave comes to life!'这三个冗余键,剩余条目均为有效内容,完全符合预期需求。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 15:32:55