簡體   English   中英

如何使用python識別另一個字符串之后是否存在特定字符串

[英]How to identify whether a specific string present after another string using python

 sentence =  "MH Diagnosis: General anxiety disorder Current MH Medications: Client takes Zoloft 25mg that he just started taking. The criteria do not include level of care recommendations for a primary diagnosis of gambling, caffeine, or nicotine disorders."

diagnosis_keywords: ["Diagnosis:", "primary diagnosis of"]
extraction keywords: ["General anxiety disorder", "gambling, caffeine, or nicotine disorders.", "accident after 12 hours"]

for i in diagnosis_keywords:
    if i in sentence:
        # How to check whether the "General anxiety disorder" present after "Diagnosis:" keyword in sentence if True then print Diagnosis: General anxiety disorder


Required solution: Diagnosis: General anxiety disorder, Diagnosis: Gambling, caffeine, or nicotine disorders.

有什么辦法可以找出診斷關鍵字后是否存在提取關鍵字。 如果是真的,我需要以格式提取解決方案,例如:“診斷:一般焦慮症”

我需要檢查在任何診斷關鍵字之后出現的一般焦慮症:[“診斷:”,“初步診斷”]。 如果真打印 診斷:一般性焦慮症。

  • 如果您不需要高性能,這里有一個簡單的解決方案:

示例代碼:

sentence =  "MH Diagnosis: General anxiety disorder Current MH Medications: Client takes Zoloft 25mg that he just started taking. The criteria do not include level of care recommendations for a primary diagnosis of gambling, caffeine, or nicotine disorders."

diagnosis_keywords = ["Diagnosis:", "primary diagnosis"]
extraction_keywords = ["General anxiety disorder", "of gambling, caffeine, or nicotine disorders.", "accident after 12 hours"]

for dia_word in diagnosis_keywords:
    for extra_word in extraction_keywords:
        if (dia_word+" "+extra_word) in sentence:
            print(dia_word+" "+extra_word)

結果:

Diagnosis: General anxiety disorder
primary diagnosis of gambling, caffeine, or nicotine disorders.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM