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

遍历长度可变的嵌套字典列表提取指定键值对(Pandas场景)

问题

需要遍历长度可变的嵌套字典列表,提取特定键值对。给定示例列表list1如下:

list1=[{'value': [{'children': [{'children': [{'children': [{'children': [{'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, {'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller', 'name': 'controller'}, {'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate', 'name': 'delegate'}, {'children': [{'coupling': {'category': 'A', 'value': '0'}, 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java', 'metric': 'coupling', 'name': 'IncentiveRateJPARepositoryImpl.java'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl', 'name': 'impl'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src', 'name': 'src'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv', 'name': 'vvv'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz', 'name': 'xyz'}], 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271', 'name': '89224495-b8b3-43c9-ac8d-addb755ab271'}]}]

需要生成目标列表list2:

list2=[
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/IncentiveRateController.java', 'metric': 'coupling', 'name': 'IncentiveRateController.java'}, 
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/controller/MaintainIncentiveController.java', 'metric': 'coupling', 'name': 'MaintainIncentiveController.java'},
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/delegate/IncentiveRateDelegate.java', 'metric': 'coupling', 'name': 'IncentiveRateDelegate.java'}, 
{'category': 'A', 'value': '0', 'file_path': '89224495-b8b3-43c9-ac8d-addb755ab271/xyz/vvv/src/impl/IncentiveRateJPARepositoryImpl.java', 'metric': 'coupling', 'name': 'IncentiveRateJPARepositoryImpl.java'}]

注:该列表为Pandas DataFrame中的一列,需实现上述转换。

解决方案

可以通过递归遍历的方式处理可变深度的嵌套结构,精准提取目标字段:

实现思路

  1. 定义递归函数,自动适配列表或字典类型的输入:
    • 若当前元素包含coupling键,判定为目标节点,将coupling内的category、value与当前节点的file_path、metric、name整合为新字典,加入结果列表。
    • 若当前元素包含children或value键,递归遍历对应的子结构,继续查找目标节点。
  2. 针对Pandas DataFrame列的场景,使用apply方法逐行调用转换函数,完成批量处理。

示例代码

import pandas as pd

def extract_target_items(nested_data):
    result = []
    # 处理列表类型的嵌套结构
    if isinstance(nested_data, list):
        for item in nested_data:
            result.extend(extract_target_items(item))
    # 处理字典类型的嵌套结构
    elif isinstance(nested_data, dict):
        # 提取目标节点数据
        if 'coupling' in nested_data:
            new_item = {
                'category': nested_data['coupling']['category'],
                'value': nested_data['coupling']['value'],
                'file_path': nested_data['file_path'],
                'metric': nested_data['metric'],
                'name': nested_data['name']
            }
            result.append(new_item)
        # 递归遍历子节点
        if 'children' in nested_data:
            result.extend(extract_target_items(nested_data['children']))
        if 'value' in nested_data:
            result.extend(extract_target_items(nested_data['value']))
    return result

# 测试单个列表转换
list2 = extract_target_items(list1)
print(list2)

# 处理Pandas DataFrame列
# 假设df为目标DataFrame,列名为'nested_list'
df['extracted_list'] = df['nested_list'].apply(extract_target_items)

这段代码会自动遍历所有层级的嵌套结构,生成符合要求的目标列表;针对DataFrame列的处理也能高效完成批量转换。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 19:55:10