Python Replace Many

admin, 24 февраля 2023 г. 12:24


>>> from functools import reduce

>>> def xreplace(s, srch, rplc, count=-1):
...     return reduce(lambda p, e: p.replace(e, rplc, count), srch, s)

>>> xreplace('are you mango', 'aeiou', '-', 1)
'-r- y-- mango'

>>> xreplace('are you mango', 'aeiou', '-')
'-r- y-- m-ng-'