Replace all the occurrences of specific words

前端 未结 4 1844
暗喜
暗喜 2020-12-11 02:00

Suppose that I have the following sentence:

bean likes to sell his beans

and I want to replace all occurrences of specific words with other

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-11 02:32

    I know its been a long time but Does this look much more elegant? :

    reduce(lambda x,y : re.sub('\\b('+y[0]+')\\b',y[1],x) ,[("bean","robert"),("beans","cars")],"bean likes to sell his beans")
    

提交回复
热议问题