Python 3.x 如何检查列表中是否存在字典值并替换

Python 3.x 如何检查列表中是否存在字典值并替换,python-3.x,Python 3.x,我想检查名词短语中的res_dict值ncp1和ncp2,如果找到,请使用其replacedPhrase键进行关联。有了上面的代码片段,我只能替换一个键,下面是ans: nounphrase = ['At the same time, other ncp1 arose, such as computers made by DEC, ncp2 , mainly for use by businesses.'] res_dict = {'rep_sentence':

我想检查名词短语中的res_dict值
ncp1
ncp2
,如果找到,请使用其replacedPhrase键进行关联。有了上面的代码片段,我只能替换一个键,下面是ans:

nounphrase = ['At the same time, other ncp1 arose, such as computers made by DEC, ncp2 , mainly 
               for use by businesses.']

res_dict = {'rep_sentence': 'At the same time, other ncp1 arose, such as computers made by DEC, 
                             ncp2 , mainly for use by businesses',
            'replacements': [{'replacedPhrase': 'desktop systems and workstations',
                              'replacement': 'ncp1'},
                             {'replacedPhrase': 'Sun, and SGI', 'replacement': 'ncp2'}]}

for each_rep in res_dict['replacements']:
    res = [masked_nounphrase for masked_nounphrase in noun_phrase if each_rep['replacement'] in 
            masked_nounphrase]
    final_result = [sub.replace(each_rep['replacement'],each_rep['replacedPhrase']) for sub in 
                    res] 
    print(final_result)

一种方法是,使用列表
名词短语的修改值更新
str

['At the same time, other desktop systems and workstations arose, such as computers made by DEC, ncp2 , mainly for use by businesses.']
输出:

s = ''
for elem in res_dict['replacements']:
    s = "".join([x.replace(elem['replacement'], elem['replacedPhrase']) if elem['replacement'] in x else x for x in noun_phrase])
    noun_phrase = [s]
print(s)  

我想检查名词短语中的res_dict值“ncp1”和“ncp2”,如果找到,请将其与其replacedPhrase键关联。有了上面的代码片段,我可以只替换一个键,并且我得到了下面的答案。。[“与此同时,出现了其他桌面系统和工作站,如DEC、ncp2生产的主要供企业使用的计算机。”]输出结果应该是什么?它正在以失败告终
At the same time, other desktop systems and workstations arose, such as computers made by DEC, Sun, and SGI , mainly for use by businesses.