[Python-ideas] str.rreplace

Georg Brandl g.brandl at gmx.net
Sat Jan 25 08:55:36 CET 2014


Am 25.01.2014 08:16, schrieb Serhiy Storchaka:
> 24.01.14 23:04, MRAB написав(ла):
>> On 2014-01-24 20:48, Chris Angelico wrote:
>>> On Sat, Jan 25, 2014 at 7:33 AM,
>>> <random832 at fastmail.us> wrote:
>>>>>>> 'aaa'[::-1].replace('aa'[::-1],'x'[::-1])[::-1]
>>>> 'ax'
>>>
>>> It makes me happy when the [::-1] smiley gets used that many times to
>>> solve a problem. Very happy.
>>>
>>> Happy that it isn't in _my_ code, to be precise...
>>>
>> It's probably not as efficient, either!
> 
> Of course it is less efficient than hypothetical rreplace, but I suppose 
> it is most efficient way in current Python.

There was also the suggestion on stackoverflow of

'x'.join('aaa'.rsplit('aa', 1))

which might be faster and less colon-y, but is very good at covering up the
real purpose of the code :)

Georg



More information about the Python-ideas mailing list