Python 3.9.7递归函数拼接嵌套JSON指定键所有值的问题
嵌套JSON中提取重复键的所有值问题
在Python 3.9.7中,尝试从字典与列表嵌套组成的JSON对象里,提取某个多次出现的键的所有值。目前仅能获取单个值,无法正确拼接所有匹配到的值,以下是相关代码及尝试过程:
参数说明
current_collection:当前待遍历的集合inner_key:需要获取值的目标键(例:"inner_key": 待获取的值)saving_value:首次调用时传入的空字符串,用于存储目标键的所有值
初始代码(无法完成拼接)
def get_inner_value_nested_collections(current_collection, inner_key, saving_value): if isinstance(current_collection, dict): for current_value in current_collection: if current_value == inner_key: saving_value += str(current_collection[current_value]) + "\n" # 打印current_collection[current_value]能得到目标值,但saving_value未完成拼接 else: if isinstance(current_collection[current_value], dict) or isinstance(current_collection[current_value], list): get_inner_value_nested_collections(current_collection[current_value], inner_key, saving_value) elif isinstance(current_collection, list): for current_value in current_collection: if isinstance(current_value, dict) or isinstance(current_value, list): get_inner_value_nested_collections(current_value, inner_key, saving_value)
尝试添加return后的代码(仍无法得到正确结果)
def get_inner_value_nested_collections(current_collection, inner_key, saving_value): if isinstance(current_collection, dict): for current_value in current_collection: if current_value == inner_key: saving_value += str(current_collection[current_value]) + "\n" return saving_value # 打印current_collection[current_value]能得到目标值,但saving_value未完成拼接 else: if isinstance(current_collection[current_value], dict) or isinstance(current_collection[current_value], list): return get_inner_value_nested_collections(current_collection[current_value], inner_key, saving_value) elif isinstance(current_collection, list): for current_value in current_collection: if isinstance(current_value, dict) or isinstance(current_value, list): return get_inner_value_nested_collections(current_value, inner_key, saving_value)
问题根源分析
- 初始代码中,字符串是不可变类型,递归调用时修改的是局部的
saving_value,外层变量不会同步更新;同时递归调用没有接收返回值,导致嵌套结构里匹配到的值无法拼接到外层的结果中。 - 添加return后的代码,一找到匹配键就直接返回,终止了后续遍历,只能拿到第一个匹配值;且遍历列表/字典元素时,遇到第一个嵌套结构就返回,后续元素完全没被处理。
修正后的代码
def get_inner_value_nested_collections(current_collection, inner_key): saving_value = "" if isinstance(current_collection, dict): # 用items()同时获取键和值,代码更清晰 for key, value in current_collection.items(): if key == inner_key: saving_value += str(value) + "\n" # 简化嵌套类型判断 elif isinstance(value, (dict, list)): # 接收递归返回的结果并拼接 saving_value += get_inner_value_nested_collections(value, inner_key) elif isinstance(current_collection, list): for item in current_collection: if isinstance(item, (dict, list)): saving_value += get_inner_value_nested_collections(item, inner_key) # 遍历完成后返回所有拼接好的值 return saving_value
使用示例
# 测试嵌套数据 test_json = { "name": "test", "target_key": "value1", "details": [ {"target_key": "value2"}, {"sub": {"target_key": "value3"}}, ["ignore", {"target_key": "value4"}] ] } # 提取所有target_key的值 result = get_inner_value_nested_collections(test_json, "target_key") print(result)
输出结果:
value1 value2 value3 value4
内容的提问来源于stack exchange,提问作者snowbreacker
相关产品推荐
相关产品推荐

