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)
我想检查名词短语中的res_dict值ncp1
和ncp2
,如果找到,请将其与其替换的Phrase键关联。有了上面的代码片段,我只能替换一个密钥,我得到了下面的ans:
['At the same time, other desktop systems and workstations arose, such as computers made by DEC, ncp2 , mainly for use by businesses.']
一种方法是,用列表noun_phrase
:的修改值更新str
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)
输出:
At the same time, other desktop systems and workstations arose, such as computers made by DEC, Sun, and SGI , mainly for use by businesses.