[英]Store each character of a String in a list
I have a list object of Strings as given below: 我有一个字符串列表对象,如下所示:
s = ['ABC','DEF','GHI','JKL']
I want to store the individual characters as a list of list items separately as below: 我想将各个字符分别存储为列表项列表,如下所示:
output = [['A','B','C'],['D','E','F'],['G','H','I'],['J','K','L']]
I know I can extract the individual items using their indexes. 我知道我可以使用它们的索引提取单个项目。 But am not able to store them in the above given manner. 但是无法以上述方式存储它们。 Could someone please help me out..! 有人可以帮帮我..!
use a list comprehension: 使用列表理解:
In [24]: s = ['ABC','DEF','GHI','JKL']
In [25]: lis=[list(x) for x in s]
In [26]: lis
Out[26]: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L']]
or use map()
: 或使用map()
:
In [27]: lis1=map(list,s)
In [28]: lis1
Out[28]: [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K', 'L']]
您可以从str
list
:
output = map(list, s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.