Reputation: 42329
I need to create a script to accept/reject some text based on whether a list of strings is present in it.
I have a list of keywords that should be used as a rejection mechanism:
k_out = ['word1', 'word2', 'some larger text']
If any of those string elements is found in the list I present below, the list should be marked as rejected. This is the list that should be checked:
c_lst = ['This is some text that contains no rejected word', 'This is some larger text which means this list should be rejected']
This is what I've got:
flag_r = False
for text in k_out:
for lst in c_lst:
if text in lst:
flag_r = True
is there a more pythonic way of going about this?
Upvotes: 4
Views: 155
Reputation:
You can use any
and a generator expression:
>>> k_out = ['word1', 'word2', 'some larger text']
>>> c_lst = ['This is some text that contains no rejected word', 'This is some larger text which means this list should be rejected']
>>> any(keyword in string for string in c_lst for keyword in k_out)
True
>>>
Upvotes: 6