如何从另一个列表中删除空列表?

How can I remove empty lists from within another list?

在 运行 文本之后通过此代码:

import re


def text_manipulator(text):
    paras = re.split('\n|\n ', text)
    item_number = 0
    for item in paras:
        item_replace = re.split('(?<=[.!?]) +', item)
        paras[item_number] = item_replace
        item_number += 1
    fixed_paras = [x for x in paras if x]
    return fixed_paras

我只剩下这个了。

[["Not a postal worker, but I'm good friends with the one on my route,"], [''], ['He has helped me through some tough times (just a nice guy to talk to)'], [''], ['I often offer him a cold gallon of water or a energy drink, he seems to really enjoy.', 'He is a real down to earth guy.']]

我可以做些什么来解决这个问题?:

[["Not a postal worker, but I'm good friends with the one on my route,"], ['He has helped me through some tough times (just a nice guy to talk to)'], ['I often offer him a cold gallon of water or a energy drink, he seems to really enjoy.', 'He is a real down to earth guy.']]

提前致谢

根据 any(iterable) 的文档:

Return True if any element of the iterable is true. If the iterable is empty, return False.

因此,当将字符串列表传递给 Any 时,如果列表中的所有元素都是空字符串,那么它将 return False 因为空字符串等同于 False.

因此在您的代码中将倒数第二行替换为:

fixed_paras = [x for x in paras if any(x)]

也将删除包含空字符串的列表。

注意:此答案基于juanpa.arrivillaga的评论