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

Python如何将一维句子列表转换为单句子子列表组成的二维列表

列表结构转换实现方案

你的原始lists是一个二层嵌套列表,外层仅包含1个元素,该元素是存储了3个问句字符串的一维列表,要将每个字符串转为独立单元素子列表,可通过以下方式实现:

最简适配写法

针对你当前给出的数据结构,直接遍历内层的字符串列表,逐个包装为单元素子列表即可:

lists = [['Was the indus valley part of the maharashtra empire?',
  'Is there such a thing as golden age of indian culture?',
  'Is the vedic period the same as the golden age?']]

new_list = [[sentence] for sentence in lists[0]]

运行后得到的new_list和你预期的结构完全一致。

基础循环写法

如果需要更直观的逻辑呈现,可以用普通for循环实现相同效果:

new_list = []
for sentence in lists[0]:
    new_list.append([sentence])

通用兼容写法

如果后续原始列表的外层包含多个存储字符串的子列表,需要统一提取所有字符串转为独立子列表,可以使用两层遍历的写法,无需手动指定内层列表索引,适配性更强:

new_list = [[item] for sub_list in lists for item in sub_list]

该写法在你当前的单内层列表场景下同样可以正常运行。


内容的提问来源于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.30 23:45:42