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

Pharo中展开嵌套字典:如何提取内层Dictionary内容?

获取嵌套字典的内层内容

这事儿其实超简单,分两种情况来处理就行:

  • 已知外层字典的键时(推荐)
    既然你已经明确外层字典的键是'CMFireAutomataModel',直接通过键索引就能拿到内层字典,这是最直接稳妥的方式:

    # 先定义你的嵌套字典
    nested_dict = {
        'CMFireAutomataModel': {
            'nbAshes': 193,
            'nbFires': 851
        }
    }
    
    # 提取内层字典
    inner_dict = nested_dict['CMFireAutomataModel']
    print(inner_dict)  # 输出: {'nbAshes': 193, 'nbFires': 851}
    
  • 不知道外层键,且外层只有一个键时
    如果外层字典只有这一个键,也可以直接取它的第一个值,不用硬编码键名:

    inner_dict = next(iter(nested_dict.values()))
    print(inner_dict)  # 同样得到目标内层字典
    

简单来说,第一种方法因为你已经知道外层键,所以优先用它,不容易出错~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:45:38