在python中将列表n项合并为一项
我有一个列表,其中每个项目都包含一个相当长的字符串,例如:在python中将列表n项合并为一项,python,string,list,Python,String,List,我有一个列表,其中每个项目都包含一个相当长的字符串,例如: mylist = [ 'this is my string number one it is much longer than that', 'this is my string two it is much longer than that', # ... ] 我想把这个列表的所有索引中的所有项目都加入到 只有一个项目,因此指数从0到n的项目应全部合并 索引0。我试过这个( )但它对我不起作用,我没有得到任何
mylist = [
'this is my string number one it is much longer than that',
'this is my string two it is much longer than that',
# ...
]
我想把这个列表的所有索引中的所有项目都加入到
只有一个项目,因此指数从0到n的项目应全部合并
索引0。我试过这个(
)但它对我不起作用,我没有得到任何错误,但我的列表项是
没有合并
我还尝试了以下代码:
final = []
for item in mylist:
final.append(''.join(item))
但是它仍然给我相同的输出,根本没有连接的列表项您想将
str.join()
应用于整个列表,而不是每个项目:
final = [''.join(mylist)]
我已将结果与一项一起放回列表中
另一个问题是试图连接输入列表中的一些元素,这就是为什么str.join()
应用于x[3:6]
的原因