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

如何将转换逻辑应用到JSON文件内的所有数据元素?

解决方案

你当前代码只修改了下标为0的第一条行星数据的首条records记录,要全量处理所有数据,嵌套遍历外层行星列表、内层records列表即可,修改后代码如下:

import json

with open('planets.json', 'r') as f:
    data = json.loads(f.read())

# 遍历所有行星条目
for item in data:
    # 遍历当前行星的所有records记录
    for idx in range(len(item['records'])):
        item['records'][idx] = list(item['records'][idx].values())

print(data)

如果你期望的输出里后两项确实是大括号包裹的集合类型,把上述代码里的list替换为set即可匹配你的预期输出格式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 22:45:09