mardi 3 septembre 2019

If a certain word is not before the search word then add to list python

I would like the program to detect whether a certain word is before the search word and if it is not to add it to a list.

This is what I have come up with myself:

sentence = "today i will take my dog for a walk, tomorrow i will not take my dog for a walk"

all = ["take", "take"] 

all2= [w for w in all if not(re.search(r'not' + w + r'\b', sentence))]
print(all2)

The excpected output is ["take"], but it remains the same with ["take, "take]

Aucun commentaire:

Enregistrer un commentaire