Python 在字符串列表中查找字符串列表,返回布尔值

Python 在字符串列表中查找字符串列表,返回布尔值,python,regex,Python,Regex,我试图在python中处理字符串列表,但不知何故,我找不到一个好的解决方案。我想在字符串列表中查找字符串列表并返回布尔值: import re sentences = ['Hello, how are you?', 'I am fine, how are you?', 'I am fine too, thanks'] bits = ['hello', 'thanks'] re.findall(sentences, bits) # desir

我试图在python中处理字符串列表,但不知何故,我找不到一个好的解决方案。我想在字符串列表中查找字符串列表并返回布尔值:

import re
sentences = ['Hello, how are you?',
             'I am fine, how are you?',
             'I am fine too, thanks']
bits = ['hello', 'thanks']

re.findall(sentences, bits)

# desired output: [True, False, True]
如果句子字符串包含一个或多个位,我想得到一个带True的布尔值数组。我也试过了

bits = r'hello|thanks'

但我总是得到错误“unhabable type:“list”。我尝试将列表转换为数组,但错误只是显示“unhabable type:”列表“”。如果有任何帮助,我将不胜感激

一个选项是使用嵌套列表:

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]
如果要使用正则表达式,则需要使用管道字符连接
,但仍需要分别检查
句子
中的每个句子

[bool(re.search('|'.join(bits), s, re.IGNORECASE)) for s in sentences]
# returns:
[True, False, True]

您的示例都是纯字母文本,没有实际的正则表达式,因此您可以在以下句子中使用
if word:
。对于涉及检查匹配开始/结束/包含特殊字符的整个单词的通用场景的正则表达式解决方案,请参阅。