【问题标题】:How to replace multiple words in a sentence using python如何使用python替换句子中的多个单词
【发布时间】:2021-10-20 22:53:19
【问题描述】:
present_text="REASON FOR VISIT Referred by. Elevated PSA. HISTORY OF PRESENT ILLNESS JAMES EVERING is an 81 year old male. CURRENT MEDICATION  AmLODIPine Besylate 5 MG Tablet 90 days, 0 refills PREVIOUS THERAPY: History of education and instructions"
a = ["HISTORY OF PRESENT ILLNESS","CURRENT MEDICATION","ENCOUNTER NOTES","PREVIOUS THERAPY"]
for i in a:
    if i in present_text:
         first_update = present_text.replace(i,i+":")
         print(first_update)

Required soln: REASON FOR VISIT Referred by. Elevated PSA. HISTORY OF PRESENT ILLNESS: JAMES EVERING is an 81 year old male. CURRENT MEDICATION:  AmLODIPine Besylate 5 MG Tablet 90 days, 0 refills PREVIOUS THERAPY: History of education and instructions

有没有什么办法可以用“”:“”在句子“”HISTORY OF PRESENT ILLNESS:“”和“”CURRENT MEDICATION:“”和“PREVIOUS THERAPY:”组成句子,不需要添加“” : "" 因为它已经存在于句子中

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您需要使用正则表达式替换函数来获取所有字符串的更改。

    import re
    
    present_text="REASON FOR VISIT Referred by. Elevated PSA. HISTORY OF PRESENT ILLNESS JAMES EVERING is an 81 year old male. CURRENT MEDICATION  AmLODIPine Besylate 5 MG Tablet 90 days, 0 refills PREVIOUS THERAPY: History of education and instructions"
    
    a = ["REASON FOR VISIT","HISTORY OF PRESENT ILLNESS","CURRENT MEDICATION","ENCOUNTER NOTES","PREVIOUS THERAPY"]
    
    for i in a:
    
       present_text= re.sub(i, "\n"+i+":", present_text)
    

    【讨论】:

    • 它正在一一更新,我需要用现在的疾病史:和当前药物:一句话来解决
    • 在之前的治疗中无需添加:因为它已经存在于句子中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2017-02-19
    • 2015-09-11
    • 2022-11-27
    相关资源
    最近更新 更多