a = "The process maps are similar to Manual Excellence Process Framework (MEPF)"
input = "流程圖類似于手動卓越流程框架 (MEPF)"
輸出 = 手動卓越流程框架 (MEPF)
我想撰寫一個 python 腳本,在那里我有那段文本,從中我想提取括號內給定首字母縮略詞的(MEPF)全部內容,完整形式是Manual Excellence Process Framework我只想通過匹配括號內的每個大寫字母來追加完整。
我的想法是,當括號內出現首字母縮略詞時,它將映射每個大寫字母,例如 (MEPF) 從最后一個字母 F 開始,它將匹配括號前的最后一個單詞,這里是框架,然后是 P (Pocess) 然后是 E(Excellence) 最后M(手動)所以最終輸出將是完整的(手動卓越流程框架)你能嘗試一次這種方式,這對我真的很有幫助
uj5u.com熱心網友回復:
使用一個簡單的正則運算式和一些后處理:
a = "I like International Business Machines (IBM). The Manual Excellence Process Framework (MEPF)"
import re
m = re.findall(r'([^)] ) \(([A-Z] )\)', a)
out = {b: ' '.join(a.split()[-len(b):]) for a,b in m}
out
輸出:
{'IBM': 'International Business Machines',
'MEPF': 'Manual Excellence Process Framework'}
如果您想檢查首字母縮略詞是否與單詞實際匹配:
out = {b: ' '.join(a.split()[-len(b):]) for a,b in m
if all(x[0]==y for x,y in zip(a.split()[-len(b):], b))
}
例子
a = "No match (ABC). I like International Business Machines (IBM). The Manual Excellence Process Framework (MEPF)."
m = re.findall(r'([^)] ) \(([A-Z] )\)', a)
{b: ' '.join(a.split()[-len(b):]) for a,b in m
if all(x[0]==y for x,y in zip(a.split()[-len(b):], b))
}
# {'IBM': 'International Business Machines',
# 'MEPF': 'Manual Excellence Process Framework'}
uj5u.com熱心網友回復:
我把你的問題作為一個挑戰我是一個初學者所以我希望這個答案對你有用并感謝你的問題:
a = "process maps are similar to Manual Excellence Process
Framework (MEPF)"
full = ''
ind = a.index('(')
ind2 = a.index(')')
acr = a[ind 1:ind2]
for i in a.split():
for j in range (len(acr)):
if acr[j] == i[0] and len(i) > 1:
word = i
full = full word ' '
print(full)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/392874.html
上一篇:計算火車能否過橋
