如果我有一个包含某些句子和单词的列表,例如:
res = ['Today is a great day', 'lunch @myplace', 'make sure to check this link: https://']
我想只删除以'@'开头的单词或包含'https'的单词,而不是删除包含该单词的整个句子,我该如何去做?现在,我有以下内容:
words_filtered = [e.lower() for e in res]
words_cleaned = [word for word in words_filtered if 'http' not in word and not word.startswith('@')]
打印words_cleaned时,确实已从列表中删除了单词,但整个句子也是如此。它返回['今天是美好的一天']但我希望它返回['今天是美好的一天','午餐','请务必查看此链接:']
答案 0 :(得分:3)
赞美理解的力量:
case2
这会产生
res = ['Today is a great day', 'lunch @myplace', 'make sure to check this link: https://']
words_cleaned = [" ".join([
words for words in sentence.split()
if 'https:' not in words and not words.startswith('@')])
for sentence in res]
print(words_cleaned)
<小时/> 或者,如
['Today is a great day', 'lunch', 'make sure to check this link:']
所述,请使用
@jpp