【发布时间】:2020-08-19 05:10:51
【问题描述】:
通过此代码运行文本后:
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.']]
提前致谢
【问题讨论】:
-
该列表不为空。但是,其中的项目是空的。所以
[x for x in paras if any(x)]可以工作,注意,也会过滤类似['',''] -
@juanpa.arrivillaga 非常感谢您的帮助!你介意解释一下这段代码是如何工作的吗?如果你有时间,干杯
标签: python list reddit editing python-re