简体   繁体   English

将字符串的每个字符存储在列表中

[英]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.

相关问题 访问列表索引中字符串中的每个字符 - Accessing each character in a string in an index in a list 使用列表中的索引替换字符串中的每个字符 - Replacing each character in a string using an index into a list 拆分列表中的每个字符串并存储在多个数组中 - Split each string in list and store in multiple arrays 如何使用字符串中的每个字符表示列表中每个整数的结果? - How to use each character in a string to represent the outcome for each integer in a list? 在字符串列表中,对于每个字符串,在特定字符之后删除字符串的一部分 - In a list of strings, for each string remove part of a string after a specific character 查找列表中每个字符串的字符出现的次数(python) - Finding the number of occurances of a character of each string within a list (python) 如何在字符串列表的每个元素的开头添加一个字符 - how to add a character at the beginning of each element of a string list 比较字符串列表中字符串中每个字符的最快方法 - Fastest method to compare each character in string in a list of strings 为什么 split() 不分隔列表 output 中字符串中的每个字符? - Why is split() not separating each character in a string in the list output? 将字符串的每个字符转换为位列表并将每个列表附加到列表列表 - Convert each character of a string to a list of bits and append each list to a list of lists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM