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

如何在Python中将三个不等长列表转换为指定格式的JSON?

正确转换列表为指定JSON格式的解决方案

问题背景

现有三个列表:

filtered_headings = ['Educational Institutions', 'Number of students', 'Number of teaching staffs', 'Number of non- teaching staffs']
sub_headings = ['College/University', 'Basic Level', 'Secondary Level', 'Secondary Level', 'College/University', 'Basic Level', 'Secondary Level', 'College/University', 'Basic Level', 'Basic Level', 'College/University', 'Secondary Level']
values = ['2', '10', '12', '566', '400', '799', '355', '115', '12', '115', '11', '11']

需要转换为如下格式的JSON:

{
 "Educational Institutions": {
  "College/University": "2",
  "Basic Level": "10",
  "Secondary Level": "12"
 },
 "Number of students": {
  "Secondary Level": "566",
  "College/University": "400",
  "Basic Level": "799"
 },
 "Number of teaching staffs": {
  "Secondary Level": "355",
  "College/University": "115",
  "Basic Level": "12"
 },
 "Number of non- teaching staffs": {
  "Basic Level": "115",
  "College/University": "11",
  "Secondary Level": "11"
 }
}

但运行原代码后,所有主标题下的内容都重复最后三个值,错误输出如下:

{
 "Educational Institutions": {
  "College/University": "11",
  "Basic Level": "115",
  "Secondary Level": "11"
 },
 "Number of students": {
  "College/University": "11",
  "Basic Level": "115",
  "Secondary Level": "11"
 },
 "Number of teaching staffs": {
  "College/University": "11",
  "Basic Level": "115",
  "Secondary Level": "11"
 },
 "Number of non- teaching staffs": {
  "College/University": "11",
  "Basic Level": "115",
  "Secondary Level": "11"
 }
}

原代码:

import json

result = {}
for indv_heading in filtered_headings:
    data_dict = {sub_heading: value for sub_heading,
                 value in zip(sub_headings, values)}
    result[indv_heading] = data_dict
json_data = json.dumps(result, indent=1)
print(json_data)

错误原因

  1. 字典键覆盖问题:字典的键具有唯一性,当sub_headings中出现重复键时,后面的键值对会覆盖前面的,最终data_dict只会保留每个重复键的最后一次赋值结果。
  2. 全量循环错误:原代码每次循环都遍历全部sub_headings和values生成字典,导致每个主标题都绑定了同一个全量处理后的字典,而非对应分组的内容。

从列表长度关系来看,filtered_headings有4个元素,sub_headings和values各有12个元素,正好是4组×3个元素,每组对应一个主标题的子项。

修正后的代码

import json

filtered_headings = ['Educational Institutions', 'Number of students', 'Number of teaching staffs', 'Number of non- teaching staffs']
sub_headings = ['College/University', 'Basic Level', 'Secondary Level', 'Secondary Level', 'College/University', 'Basic Level', 'Secondary Level', 'College/University', 'Basic Level', 'Basic Level', 'College/University', 'Secondary Level']
values = ['2', '10', '12', '566', '400', '799', '355', '115', '12', '115', '11', '11']

result = {}
# 按每组3个元素拆分sub_headings和values
group_size = 3
for i, heading in enumerate(filtered_headings):
    # 计算当前组的起始和结束索引
    start = i * group_size
    end = start + group_size
    # 取当前组的子标题和对应值生成字典
    data_dict = {sub: val for sub, val in zip(sub_headings[start:end], values[start:end])}
    result[heading] = data_dict

json_data = json.dumps(result, indent=1)
print(json_data)

代码说明

  • 先确定每组的大小为3(因为4个主标题对应12个元素,12/4=3)。
  • 通过enumerate遍历filtered_headings,同时获取索引i,计算当前主标题对应的子标题和值的切片范围[start:end]。
  • 对每组切片后的子标题和值生成字典,避免了键覆盖问题,同时每个主标题绑定对应分组的内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 15:52:43