感谢大家的帮助,我完成了这一步,但我有一些新问题,我希望有人能帮助我,我找到了以下you单词,并想用另一个单词替换它们,但此代码仅适用于第一个单词
import Re
text = 'Suddenly you said goodbye, even though I was passionately in love with you, I hope you stay lonely and live for a hundred years'
def replace(text,key,value,NumberL):
matches = list(re.finditer(key,text,re.I))
for i in NumberL:
newT = matches[i-1]
text = text[:newT.start(0)] + value + text[newT.end(0):]
print(text)
replace(text,'you','Cleis',[2,3])
结果:你突然说了声再见,尽管我非常爱克莱斯,但我"希望"孤独地生活了一百年.
我编辑并突出显示了错误词.
有人能给我一个解决这个问题的办法吗?