【问题标题】:How to split a list based on new line in the list如何根据列表中的新行拆分列表
【发布时间】:2022-01-18 13:52:25
【问题描述】:

我有这样的列表。

list =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
     '',
     'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

我们可以观察到有一个空列表''。我想根据空列表拆分我的列表。

预期输出:

#输入

input_list = ['some text ', '', 'some texts example']

输出:

list1 = ['some text ']
list2 = ['some text example']

【问题讨论】:

  • 是否需要扩展到 x 个输出列表?
  • 您需要将输出作为列表列表吗?

标签: python list


【解决方案1】:

遍历列表中的每个元素,当存在长度为 0 的元素时将其删除。

l =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
     '',
     'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

output = [ele for ele in l if len(ele) > 0]
list1, list2 = output

这适用于您的示例,但它不会在空元素上“拆分”。然后你可以使用 for 循环:

output = ['']

for ele in l:
    if len(ele) > 0:
        output[-1] += ele
    else:
        output.append('')

最后一部分拆分你的列表:

l = ['a', 'b', '', 'c']
output = ['']

for ele in l:
    if len(ele) > 0:
        output[-1] += ele
    else:
        output.append('')
# output = ['ab', 'c']

编辑:输出为列表:

l = ['a', 'b', '', 'c']
output = [[]]

for ele in l:
    if len(ele) > 0:
        output[-1].append(ele)
    else:
        output.append([])
# output = [['a', 'b'], ['c']]

如果您有更多列表,它会在此处停止,因为输出可以任意大。如果你知道结果是两个列表,你可以解压它们:

lst1, lst2 = output

旁注:不要使用变量list,因为它是python中的数据结构。

【讨论】:

  • 这是很好的解释。正如我在我的问题中提到的。输出应该是列表。但是,在你的情况下,输出是“str”
  • 我会为您编辑我的答案,但如果您了解原理,也许您可​​以自己尝试一下,将其视为您想要使用 python 和列表解决的下一个问题的学习方式。
【解决方案2】:

超级基本的解决方案。允许拆分两个以上的句子。 sentences 变量中的输出,列表列表。

list = ['a','','b','','c']
sentences = []
for s in list:
    if len(s)!=0:
        sentences.append([s])

这里的其他答案中有更好更快的解决方案,这个只是详细介绍了一个不太技术/初学者友好的解决方案。

【讨论】:

    【解决方案3】:

    如果您知道您将拥有多少个列表:

    list1, *list2 = (i for i in input_list if i)
    

    否则,如果您不知道最终会得到多少个列表,您可能会得到 dictlistlist's

    【讨论】:

      【解决方案4】:

      您可以使用列表字典。

      l =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
           '',
           'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']
      
      lists = dict()
      x = 1
      for ele in l:
        if(len(ele) > 0): # checking if the length of the element in l is sufficient (larger than 0)
          lists[x] = [ele] # adding a list with the element to the dictionary
          x += 1
      

      输出:

      lists[1] = ['正如迈克尔·哈维所写,段落“本质上是一个 标点符号的形式,和其他前者一样。 ']

      lists[2] = ['许多新手作家倾向于做出鲜明的区分 在内容和风格之间,认为一篇论文可以很强大']

      【讨论】:

        【解决方案5】:

        您可以使用下面附加的 sn-p。

        list =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
             '',
             'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']
        
        out = [[i] for i in list if i!='']
        
        # Printing each list
        for i in out:
          print(i)
        

        Run the code here

        【讨论】:

          猜你喜欢
          • 2020-04-22
          • 1970-01-01
          • 2019-01-11
          • 1970-01-01
          • 2018-09-01
          • 2011-01-05
          • 1970-01-01
          • 2018-10-24
          • 1970-01-01
          相关资源
          最近更新 更多