【问题标题】:Join on nested lists of strings加入嵌套的字符串列表
【发布时间】: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'  

我提到了onetwo 的问题,虽然这对我来说很有效,但第一个问题是这样的:

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


【解决方案1】:

我会定义一个可以在任意嵌套的字符串列表上工作的函数:

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)

【讨论】:

    【解决方案2】:

    使用您的问题 1,以下将是一个通用但不是有效的解决方案。 1.为列表的所有元素添加空间 2. 将它们与我们的小代码合并。 3.从两端修剪空间 4.删除可能已经创建的重复空间。

    【讨论】:

      【解决方案3】:

      怎么样:

      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)
      

      【讨论】:

        【解决方案4】:

        我认为最好的方法是先展平列表,然后按空格加入。

        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))
        

        或者,您可以使用@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'
        

        【讨论】:

          【解决方案5】:

          应该是:

          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 )
          

          几乎可以是:' '.join( [ ''.join(x) for x in array ] )

          你也可以使用三元:

          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 )
          

          【讨论】:

            猜你喜欢
            • 2016-02-08
            • 1970-01-01
            • 2021-11-25
            • 1970-01-01
            • 2019-07-07
            • 1970-01-01
            • 2021-08-04
            • 2019-01-09
            • 2017-04-15
            相关资源
            最近更新 更多