简体   繁体   English

加入嵌套的字符串列表

[英]Join on nested lists of strings

I have a list of lists: 我有一份清单清单:

array = ['there is nothing','there is everything,',['there is','where']]  

and here's a small piece of code: 这是一小段代码:

str2 = ""
for item in array:
    str2 += "".join(char for char in item)   

which gives : 这使 :

'there is nothingthere is everything,there iswhere'  

How could I possibly add a space between each item of the outer list as well as the inner one? 我怎么可能在外部列表的每个项目和内部列表之间添加一个空格?

The expected output string is : 预期的输出字符串是:

'there is nothing there is everything, there is where'  

I referred to questions one and two ,though that dint work in my case , the first one gives something like: 我提到了第一第二个问题,虽然这个问题在我的案例中起作用,但第一个问题是:

str2=""
lst = [' {}'.format(elem) for elem in array]
for item in lst:
    str2 += "".join(char for char in item)

output : 输出

" there is nothing there is everything, ['there is', 'where']"  

and the second one doesn't work for words. 而第二个不适用于文字。

I would define a function that can work on arbitrarily nested lists of strings: 我将定义一个可以在任意嵌套的字符串列表上工作的函数:

array = ['there is nothing','there is everything,',['there is','where']]

def concat_string(array):
    ret = ""
    for item in array:
        if isinstance(item,list):
            ret += concat_string(item)
        else:
            ret += item + " "

    return ret

print concat_string(array)

How about: 怎么样:

array = ['there is nothing','there is everything,',['there is','where']]
s = ''
for i in array:
    if isinstance(i, list):
        for j in i:
            s = ' '.join((s, j)) if s != '' else j
    else:
        s = ' '.join((s, i)) if s != '' else i
print(s)

The best way i think is to flatten the list first and then join them by space. 我认为最好的方法是先将列表展平,然后按空格加入。

import collections
def flatten(foo):
    for x in foo:
        if isinstance(x, collections.Iterable) and not isinstance(x, str):
            for y in flatten(x):
                yield y
        else:
            yield x

my_sentence = " ".join(flatten(array))

or, you can use this a single line solution as mentioned by @bhat irshad but it will not work for arbitrary nesting 或者,您可以使用@bhat irshad提到的单行解决方案,但它不适用于任意嵌套

In [1]: array = ['there is nothing','there is everything,',['there is','where']]

In [2]: " ".join([" ".join(s) if isinstance(s,list) else s for s in array])
Out[2]: 'there is nothing there is everything, there is where'

It should be: 它应该是:

array = ['there is nothing','there is everything,',['there is','where']]  
newstring = ''
for a in array :
    if type( a ) is str :
        newstring += a + " "
    else :
        newstring += ' '.join( a )

And it almost could be : ' '.join( [ ''.join(x) for x in array ] ) 它几乎可能是: ' '.join( [ ''.join(x) for x in array ] )

You can use ternary as well : 你也可以使用三元:

array = ['there is nothing','there is everything,',['there is','where']]  
newstring = ''
for a in array :
    newstring += a + " " if type( a ) is str else ' '.join( a )

using your question 1, the following would be a generic but not effient solution. 使用您的问题1,以下将是一个通用但不是有效的解决方案。 1. Add space to all elements of the list 2. merge them with our small code. 1.为列表的所有元素添加空格2.将它们与我们的小代码合并。 3. Trim spaces from both ends 4. remove duplicate spaces that may have been created. 3.从两端修剪空格4.删除可能已创建的重复空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM