Python 如何检查列表中是否包含括号内的字符串?

Python 如何检查列表中是否包含括号内的字符串?,python,string,list,for-loop,search,Python,String,List,For Loop,Search,我正在尝试使用我的代码查找包含字符串“include\u this\u titles”的标题,并过滤掉“disqualificationterms” 应仅返回: consolidated statements of operations (usd $) 但现在又回来了 consolidated statements of operations (usd $) consolidated statements of operations (parenthetical) (usd $) 您正在从小

我正在尝试使用我的代码查找包含字符串“include\u this\u titles”的标题,并过滤掉“disqualificationterms”

应仅返回:

consolidated statements of operations (usd $)
但现在又回来了

consolidated statements of operations (usd $)
consolidated statements of operations (parenthetical) (usd $)

您正在从小写术语的白名单中搜索子字符串匹配项,但正在检查大写字符串中的匹配项

这可能是您正在寻找的测试:

any(x in sheet_title.lower() for x in include_these_titles)
此外,如果你想取消资格条款,也许你想在这一步上“不做任何”。您需要精确匹配,除非您也希望在那里使用lower()

for sheet_title in titles:
    if any(x in sheet_title.lower() for x in include_these_titles):
        if not any(x in sheet_title for x in disqualifying_terms):
            print(sheet_title)

它应该返回什么?只是合并经营报表(usd$)我想过滤掉任何带有括号的内容。你能给出一个预期产出的例子吗?合并经营报表(usd$)请将预期产出编辑到问题中。当我试着运行你的代码时,我发现工作表标题没有定义。我确实调整了较低的标题。我想如果没有任何解决办法的话!我不知道你能这么做,非常感谢!
for sheet_title in titles:
    if any(x in sheet_title.lower() for x in include_these_titles):
        if not any(x in sheet_title for x in disqualifying_terms):
            print(sheet_title)