简体   繁体   中英

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() :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM