如何将字符串列表转换为子列表列表,每个子列表中的每个字符串? [英] How do you turn a list of strings into a list of sublists with each string in each sublist?

查看:47
本文介绍了如何将字符串列表转换为子列表列表,每个子列表中的每个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串列表转换为字符串子列表?

How do you turn a list of strings into a list of sublist of strings?

例如:

List_of_Strings = ['abc','def','ghi']

所需的输出:

[['abc'],['def'],['ghi']]

我的破解方法是:

List_of_Sublist_of_Strings = [(str(x)+",").split(",") for x in List_of_Strings]

产生:

[['abc', ''], ['def', ''], ['ghi', '']]

这会在子列表中产生一个不需要的空项目,但是也许不可能创建一个子列表列表,其中子列表中只有一个项目.

This produces an unwanted empty item in the sublists, but perhaps it's not possible to create a list of sublists in which the sublists only have one item.

推荐答案

您需要将这些字符串放在 [] 中,然后完成.

You need to put those strings in [] there, and it's done.

>>> lis =  ['abc','def','ghi']
>>> [[x] for x in lis]
[['abc'], ['def'], ['ghi']]

这篇关于如何将字符串列表转换为子列表列表,每个子列表中的每个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆