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

如何在嵌套字典中将指定子键值转为有序列表?

处理嵌套字典中'interpro description'的分割需求

给定一个包含大量主键的嵌套字典,需要将每个子字典里的interpro description值按逗号(带空格)分割为列表,且严格保留原有顺序。以下是具体实现方案:

示例数据

nested_dict = {
    'C4QY10_e': {
        'protein accession': 'C4QY10_e',
        'sequence length': [1879],
        'analysis': 'Pfam',
        'signature accession': 'PF18314, PF02801, PF18325, PF00109, PF01648',
        'signature description': "Fatty acid synthase type I helical domain, Beta-ketoacyl synthase, Fatty acid synthase subunit alpha Acyl carrier domain, 4'-phosphopantetheinyl transferase superfamily",
        'start location': [328, 139, 1761],
        'stop location': [528, 300, 1861],
        'e-value': [4.7e-73, 1.3e-72, 1.4e-18],
        'interpro accession': 'IPR041550, IPR040899, IPR008278',
        'interpro description': "Fatty acid synthase type I, Fatty acid synthase subunit alpha, 4'-phosphopantetheinyl transferase domain",
        'nunique': [1]
    }
}

实现代码

# 遍历嵌套字典的每个子字典
for protein_id, protein_data in nested_dict.items():
    # 检查是否存在目标键,避免KeyError
    if 'interpro description' in protein_data:
        # 按", "分割字符串,严格保留原顺序
        protein_data['interpro description'] = protein_data['interpro description'].split(', ')

处理结果验证

执行代码后,示例中子字典的interpro description会变为:

[
    "Fatty acid synthase type I",
    "Fatty acid synthase subunit alpha",
    "4'-phosphopantetheinyl transferase domain"
]

完全符合要求的顺序与元素内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 16:55:30