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

Python如何将问答嵌套列表转换为指定结构字典列表

需求可行性判断

该转换需求完全可以实现,处理逻辑非常简单,仅需遍历原始嵌套列表,将每个子列表存储的题干、答案按键值对映射为字典,最终汇总为新列表即可,不存在技术障碍。

具体实现步骤
  1. 首先注意:不要使用list作为原始数据的变量名,list是Python内置的列表类型关键字,直接用作变量名会覆盖内置方法,建议将原始数据变量重命名为qa_raw_list这类语义化名称。
  2. 选择以下任意一种写法完成转换即可:
  • 列表推导式写法(最简洁)
# 原始嵌套问答数据
qa_raw_list = [
    ['It has been noted that in __________________ urban cities quickly expanded rather than the slow evolution of urbanism in the northwest.',
      'Gujarat'],
     ['These traditions and cultures include Anarta Tradition c. 39501900 BC Padri Ware 36002000 BC PrePrabhas Assemblage 32002600 BC Pre Urban Harappan Sindh __________________ Pottery Burial Pottery 30002600 BC Black and Red Ware 3950900 BC Reserved Slip Ware 39501900 BC Micaceous Red Ware 26001600 BC.',
      'Type'],
     ['Later in the 18th century Gujarat came under control of the __________________ Empire who dominated the politics of India.',
      'Maratha']
]

# 执行转换
output = [{"question": qa_item[0], "answer": qa_item[1]} for qa_item in qa_raw_list]
  • 普通for循环写法(逻辑更直观,适合新手理解)
output = []
for qa_item in qa_raw_list:
    # 按要求组装单条问答的字典
    single_qa = {
        "question": qa_item[0],
        "answer": qa_item[1]
    }
    output.append(single_qa)

运行上述代码后得到的output变量,结构和内容和你给出的目标结果完全一致,可直接使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.28 12:31:01