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

如何对比两个嵌套字典?实现差异输出方案问询

问题:嵌套字典的差异检测实现

我有两个嵌套结构的字典:

old_state = {
    'https://github.com/search?q=i+love+python': {'title': 'i love python', 'repo_count': '517'},
    'https://github.com/search?q=python+3': {'title': 'python 3', 'repo_count': '101K'},
    'https://github.com/search?q=world': {'title': 'world', 'repo_count': '1M'},
    'https://github.com/search?q=hello+world': {'title': 'hello world', 'repo_count': '1M'}
}

new_state = {
    'https://github.com/search?q=python+3': {'title': 'python 3', 'repo_count': '102K'},
    'https://github.com/search?q=world': {'title': 'world', 'repo_count': '1M'},
    'https://github.com/search?q=hello+world': {'title': 'hello world', 'repo_count': '1M'},
    'https://github.com/search?q=i+love+python': {'title': 'i love python', 'repo_count': '517'}
}

目前我已经能实现单层字典的差异对比输出:

old_state = {'title': 'i love python', 'repo_count': '517'}
new_state = {'title': 'python 3', 'repo_count': '102K'}

if old_state['title'] != new_state['title']:
   print('new change in title')
if old_state['repo_count'] != new_state['repo_count']:
   print('new change in repo count')

现在希望对上述嵌套字典实现类似的差异检测并输出对应提示,请问是否可行?若可行,该如何实现?


解决方案

完全可行,你可以通过遍历嵌套字典的外层键,再对比内层字段的方式实现,同时还能处理键新增/删除的情况。以下是具体实现代码:

old_state = {
    'https://github.com/search?q=i+love+python': {'title': 'i love python', 'repo_count': '517'},
    'https://github.com/search?q=python+3': {'title': 'python 3', 'repo_count': '101K'},
    'https://github.com/search?q=world': {'title': 'world', 'repo_count': '1M'},
    'https://github.com/search?q=hello+world': {'title': 'hello world', 'repo_count': '1M'}
}

new_state = {
    'https://github.com/search?q=python+3': {'title': 'python 3', 'repo_count': '102K'},
    'https://github.com/search?q=world': {'title': 'world', 'repo_count': '1M'},
    'https://github.com/search?q=hello+world': {'title': 'hello world', 'repo_count': '1M'},
    'https://github.com/search?q=i+love+python': {'title': 'i love python', 'repo_count': '517'}
}

# 获取所有需要对比的键(两个字典的键的并集)
all_keys = set(old_state.keys()).union(set(new_state.keys()))

for key in all_keys:
    # 情况1:键只在旧字典中,说明被删除
    if key not in new_state:
        print(f"URL {key} 已被移除")
        continue
    # 情况2:键只在新字典中,说明是新增
    if key not in old_state:
        print(f"新增URL {key}")
        continue
    # 情况3:键在两个字典中,对比内层字段
    old_inner = old_state[key]
    new_inner = new_state[key]
    # 对比title
    if old_inner['title'] != new_inner['title']:
        print(f"URL {key} 的title发生变化:{old_inner['title']} → {new_inner['title']}")
    # 对比repo_count
    if old_inner['repo_count'] != new_inner['repo_count']:
        print(f"URL {key} 的repo_count发生变化:{old_inner['repo_count']} → {new_inner['repo_count']}")

代码说明:

  • 先通过set的并集获取所有需要检查的URL键,避免遗漏新增或删除的条目
  • 分三种情况处理每个URL键:删除、新增、存在于两个字典中
  • 对存在于两个字典中的键,复用你之前的单层字典对比逻辑,输出具体的变化内容

运行这段代码后,会输出:

URL https://github.com/search?q=python+3 的repo_count发生变化:101K → 102K

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 22:18:19