简体   繁体   English

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

[英]How to check if string including within parentheses is in list?

I am trying to use my code to find titles which include strings "include_these_titles" and filters out "disqualifying terms"我正在尝试使用我的代码查找包含字符串“include_these_titles”的标题并过滤掉“取消资格的条款”

titles = [
'Document And Entity Information (USD $)',
'Consolidated Statements of Operations (USD $)',
'Consolidated Statements of Operations (Parenthetical) (USD $)',
'General and Summary of Significant Accounting Policies'
]

include_these_titles = [
"consolidated statement of operations",
"consolidated statements of operations",
]

disqualifying_terms = ["paren","paran", "(parenthetical)", "paran", "(Parenthetical)"]

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

Should Return only:应该只返回:

consolidated statements of operations (usd $)

but currently returns但目前返回

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

You are searching for substring matches from a whitelist of lowercase terms, but you are checking for them in a capitalized string.您正在从小写术语白名单中搜索 substring 匹配项,但您正在检查大写字符串中的匹配项。

This may be the test you're looking for:这可能是您正在寻找的测试:

any(x in sheet_title.lower() for x in include_these_titles)

Additionally, if you're wanting to disqualify terms, perhaps you want "not any" on that step.此外,如果您想取消条款的资格,也许您希望在该步骤中“没有任何”。 You'll need exact matches, unless you want to use lower() there, as well.您将需要完全匹配,除非您也想在那里使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM