Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete list word combination python 3

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]
like image 765
orthoeng2 Avatar asked Feb 06 '18 20:02

orthoeng2


People also ask

How do I remove a specific word from a list in Python?

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 do I remove a specific element from a list in Python?

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.

How do we remove an item in a particular index from a list?

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.

How do I remove certain words from a string?

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.


3 Answers

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:

  • Any of the keep words are in x
  • None of the delete words are in x

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().

like image 148
pault Avatar answered Oct 04 '22 18:10

pault


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']
like image 33
Mazdak Avatar answered Oct 04 '22 19:10

Mazdak


>>> 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.

like image 21
AGN Gazer Avatar answered Oct 04 '22 17:10

AGN Gazer