【发布时间】:2018-07-17 00:11:07
【问题描述】:
我有一个字符串列表,我想搜索一个单词组合。 如果组合不存在,则删除列表。有没有python列表 理解会起作用吗?
word_list = ["Dogs love ice cream", "Cats love balls", "Ice cream", "ice cream is good with pizza", "cats hate ice cream"]
keep_words = ["Dogs", "Cats"]
Delete_word = ["ice cream"]
删除其中包含冰淇淋的单词,但如果句子中包含狗或猫,则保留它。
Desired_output = ["Dogs love ice cream", "Cats love balls", "cats hate ice cream"]
在尝试此代码时也尝试了 AND 和 OR,但无法正确组合。
output_list = [x for x in word_list if "ice cream" not in x]
【问题讨论】:
-
ice cream需要在一起吗?就像这句话会被认为是有效的:keep cream over the ice? -
我在想它在一起“冰淇淋”但很好的一点
-
最后一个是包含
cat而不是cats! -
我将编辑的好地方 Kasramvd 猫
标签: python arrays list for-loop