dimanche 15 mars 2020

Iterating through list for finding a substring in an if statement

I have a list of strings:

relations = ['displays', '1000displays', 'chooses', '1011chooses', '1020displays', 'clicks', '1031clicks', 'add to', 'checks', '1040checks', 'inserts discount offer to', '1050inserts discount offer to', 'inserts']

And a substring:

t_object = 'discount offer'

And I want to execute the code in the if block only if the string discount offer is not present in the relations list.

I have the following code but since discount offer is present in the relations, it should not step into the code block in the if statement, but it does. Why?

if len(t_object) > len(objects[sentence_number]) and (s for s in relations if t_object not in s):
     print('I am in but I shoudlt be.')

Aucun commentaire:

Enregistrer un commentaire