Python:用缩写替换列表中的语句



我有三个以给定格式的列表:

A = ['**New fund offering** documents required for opening an account in the name of a single Individual', 'Can an Individual open more than one Investment Account or folio']
B = [u'**New fund offering**', u'NFOs', u'Systematic investment plan', u'Systematic investment plans', u'SIPs', u'SWPs']
C = [u'**NFO',** u'NFO', u'SIP', u'SIP', u'SIP', u'SWP']

目的是替换列表 a 中发现的短语,如果列表 c 相应的缩写,如果列表中的短语匹配> b 。请让我知道如何在Python中做到这一点。因此,最终列表 A 将成为:

A = ['NFO documents required for opening an account in the name of a single Individual', 'Can an Individual open more than one Investment Account or folio']

您可以使用str.replace替换字符串的部分,并且可以使用in检查匹配项:

for idx_a, substring in enumerate(A):
    for part, replacement in zip(B, C):
        if part in substring:
            A[idx_a] = A[idx_a].replace(part, replacement)

最新更新