我有一个字符串列表,我想搜索单词组合。 如果组合不存在,则删除列表。有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]
答案 0 :(得分:7)
这是一个列表理解解决方案:
[x for x in word_list if any(kw.lower() in x.lower() for kw in keep_words)
or all(dw.lower() not in x.lower() for dw in Delete_word)]
# ['Dogs love ice cream', 'Cats love balls', 'cats hate ice cream']
这也为删除单词列表中的多个单词增加了灵活性。
<强>解释强>
如果以下任何一项都是True
:
我从你的例子中假设你希望这个不区分大小写,所以要对这些单词的低级版本进行所有比较。
答案 1 :(得分:5)
作为一种优化方法,您可以将keep_word
和delete_words
置于集合中并使用itertools.filterfalse()
过滤列表:
In [48]: def key(x):
words = x.lower().split()
return keep_words.isdisjoint(words) or not delete_words.isdisjoint(words)
....:
In [49]: keep_words = {"dogs", "cats"}
In [51]: delete_words = {"ice cream"}
In [52]: list(filterfalse(key ,word_list))
Out[52]: ['Dogs love ice cream', 'Cats love balls', 'cats hate ice cream']
答案 2 :(得分:1)
>>> list(filter(lambda x: not any(i in x for i in Delete_word)
... or any(i in x for i in keep_words), word_list))
['Dogs love ice cream', 'Cats love balls', 'Ice cream']
相应地修改它以实现不区分大小写的实现。