【发布时间】:2014-12-21 21:17:23
【问题描述】:
我有一个列表列表:
array = ['there is nothing','there is everything,',['there is','where']]
这里有一小段代码:
str2 = ""
for item in array:
str2 += "".join(char for char in item)
给出:
'there is nothingthere is everything,there iswhere'
我怎么可能在外部列表和内部列表的每个项目之间添加一个空格?
预期的输出字符串是:
'there is nothing there is everything, there is where'
我提到了one 和two 的问题,虽然这对我来说很有效,但第一个问题是这样的:
str2=""
lst = [' {}'.format(elem) for elem in array]
for item in lst:
str2 += "".join(char for char in item)
输出:
" there is nothing there is everything, ['there is', 'where']"
第二个对单词不起作用。
【问题讨论】:
-
在 str2 += "
".join(char for char in item) 行的引号中加一个空格 -
那不行,这是我做的第一件事
-
如果这样行得通,这个问题会有十几个答案以及几十个反对票,你可能没有机会发表评论,因为它现在已经关闭了。跨度>
-
预期的输出不应该是:'什么都没有,什么都有,有哪里'?
-
这是您简单测试用例的在线代码:" ".join([" ".join(s) if isinstance(s,list) else s for s in array]) 它应该可以工作但是,对于它,它不适用于任意嵌套。
标签: python string list python-2.7 concatenation