I have a list of strings that i would like to search for a word combination. Then delete the list if the combination is not there. Is there a python list comprehension that would work?
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"]
Delete words that have ice cream in it but if dogs or cats is in the sentence keep it.
Desired_output = ["Dogs love ice cream", "Cats love balls", "cats hate ice cream"]
Was trying this code also tried AND and OR but cannot get the combination right.
output_list = [x for x in word_list if "ice cream" not in x]
The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.
How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.
You can use the pop() method to remove specific elements of a list. pop() method takes the index value as a parameter and removes the element at the specified index.
We can use the replace() function to remove word from string in Python. This function replaces a given substring with the mentioned substring. We can replace a word with an empty character to remove it. We can also specify how many occurrences of a word we want to replace in the function.
Here's a list comprehension solution:
[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']
This also adds flexibility for multiple words in the delete words list.
Explanation
Iterate over the list and keep the word if either of the following are True
:
I presume from your example that you wanted this to be case insensitive, so make all comparison on the lower-cased versions of the words.
Two helpful functions are any()
and all()
.
As an optimized approach you can put your keep_word
and delete_words
within set and use itertools.filterfalse()
to filter the list out:
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']
>>> 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']
Modify this accordingly for a case-insensitive implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With