如何替换除第一个以外的所有事件?

How to replace all occurences except the first one?

如何替换字符串中除第一个单词外的所有重复单词?也就是这些字符串

s='cat WORD dog WORD mouse WORD'
s1='cat1 WORD dog1 WORD'

将被替换为

s='cat WORD dog REPLACED mouse REPLACED'
s1='cat1 WORD dog1 REPLACED'

我不能replace the string backward因为我不知道这个词在每一行出现了多少次。我确实想出了一个迂回的方法:

temp=s.replace('WORD','XXX',1)
temp1=temp.replace('WORD','REPLACED')
ss=temp1.replace('XXX','WORD')

但我想要一个更pythonic的方法。你有什么想法吗?

string.countrreplace

一起使用
>>> def rreplace(s, old, new, occurrence):
...     li = s.rsplit(old, occurrence)
...     return new.join(li)
... 
>>> a
'cat word dog word mouse word'
>>> rreplace(a, 'word', 'xxx', a.count('word') - 1)
'cat word dog xxx mouse xxx'