Find List Of Strings In List Of Strings, Return Boolean
I am trying to work with lists of strings in python and somehow I can't find a good solution. I want to find a list of strings within a list of strings and return boolean values: i
Solution 1:
One option is to use a nested list comprehension:
sentences = ['Hello, how are you?',
'I am fine, how are you?',
'I am fine too, thanks']
bits = ['hello', 'thanks']
[any(b in s.lower() for b in bits) for s in sentences]# returns:[True, False, True]
If you want to use a regular expression, you need to join bits
with a pipe character, but you will still need to check each sentence in sentences
individually.
[bool(re.search('|'.join(bits), s, re.IGNORECASE)) for s in sentences]
# returns:
[True, False, True]
Post a Comment for "Find List Of Strings In List Of Strings, Return Boolean"