[英]How to convert a list with strings into a 2D list
I have a list like this:我有一个这样的清单:
list = ['aa#1#ll', 'bb#2#ff', 'cc#3#ff']
I want to convert it to a 2D list in this format我想将其转换为这种格式的二维列表
list2 = [['aa', '1', 'll'], ['bb', '2', 'ff'], ['cc', '3', 'ff']]
How can I do this in Python 3?我怎样才能在 Python 3 中做到这一点?
You can use Python's split(delimiter)
method inside a generator:您可以在生成器中使用 Python 的
split(delimiter)
方法:
list2 = [x.split("#") for x in list]
Explanation: Here, for each string x
in the original list, we are calling x.split("#")
on it, which will give a list of substrings that are separated by hashes in x
, and we are adding the result to the list list2
.说明:这里,对于原始列表中的每个字符串
x
,我们对其调用x.split("#")
,它将给出一个由x
的哈希分隔的子字符串列表,我们将结果添加到列表list2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.