我有一个字符串数组,如下所示:
string = [ [ "this is a sample",
"this is another sample"],
[ "The third sample",
"the fourth one"] ]
但我想将其转换为:
string = [ "this is a sample",
"this is another sample",
"The third sample",
"the fourth one" ]
我该怎么办? 我知道我可以通过预先分配字符串并迭代来实现。但是有更简单的方法吗?
答案 0 :(得分:1)
你可以尝试使用列表理解。 代码:
string = [ [ "this is a sample",
"this is another sample"],
[ "The third sample",
"the fourth one"] ]
print([_ for i in range(len(string)) for _ in string[i]])
答案 1 :(得分:1)
也许使用列表理解。像
这样的东西string = [s for S in string for s in S]