I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.) What do you think?
On Fri, 24 Jan 2014 08:47:14 -0800 (PST)
Ram Rachum
I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied? I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace. Regards Antoine.
Yep, it differs only when count is supplied.
Yep, bytes.rreplace and bytearray.rreplace and par for the course :)
And yes, the name is annoying, but what can you do? Plus now that I think
about it the first two letters happen to be my initials, so I suggest I
should be happy :)
On Fri, Jan 24, 2014 at 6:56 PM, Antoine Pitrou
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
Regards
Antoine.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
--
--- You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group. To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/4cLkOx18u48/unsubscribe. To unsubscribe from this group and all its topics, send an email to python-ideas+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
On Fri, 24 Jan 2014 19:30:00 +0200
Serhiy Storchaka
24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace(). Overgeneralization doesn't help. Regards Antoine.
On 01/24/2014 09:36 AM, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: 24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace().
The funny look of the name, I think. ;) -- ~Ethan~
24.01.14 19:36, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: 24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace(). Overgeneralization doesn't help.
If open a door for rreplace, it would be not easy to close it for rindex and rremove.
On Fri, 24 Jan 2014 20:13:26 +0200
Serhiy Storchaka
24.01.14 19:36, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: 24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace(). Overgeneralization doesn't help.
If open a door for rreplace, it would be not easy to close it for rindex and rremove.
Perhaps you underestimate our collective door closing skills ;) Regards Antoine.
On Jan 24, 2014, at 9:43, Ethan Furman
On 01/24/2014 09:36 AM, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: 24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace().
The funny look of the name, I think. ;)
And the pronunciation. Hard to say it without sounding like a pirate. Although I guess you could interpret the rr as a rolled r: strrrrings have rrrrreplace thanks to rrrrachum. But the inclusion of rindex makes me think this was a serious suggestion to add r versions of all methods that involve searching. Which probably isn't worth the effort to do, but there's nothing really wrong with the idea.
On Jan 24, 2014, at 10:20, Antoine Pitrou
On Fri, 24 Jan 2014 20:13:26 +0200 Serhiy Storchaka
wrote: 24.01.14 19:36, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: 24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace(). Overgeneralization doesn't help.
If open a door for rreplace, it would be not easy to close it for rindex and rremove.
Perhaps you underestimate our collective door closing skills ;)
While we're speculatively overgeneralizing, couldn't all of the index/find/remove/replace/etc. methods take a negative n to count from the end, making r variants unnecessary?
On 2014-01-24 16:56, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
Not necessarily:
'aaa'.replace('aa', 'x') 'xa' 'aaa'.rreplace('aa', 'x') 'ax'
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
On Fri, Jan 24, 2014, at 14:17, MRAB wrote:
On 2014-01-24 16:56, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
Not necessarily:
'aaa'.replace('aa', 'x') 'xa' 'aaa'.rreplace('aa', 'x') 'ax'
'aaa'[::-1].replace('aa'[::-1],'x'[::-1])[::-1] 'ax'
On 24/01/2014 20:48, Chris Angelico wrote:
On Sat, Jan 25, 2014 at 7:33 AM,
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...
ChrisA
+1 -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
On 2014-01-24 20:48, Chris Angelico wrote:
On Sat, Jan 25, 2014 at 7:33 AM,
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! And if we're going to do it that way, do we really need .rindex and .rfind? Or .rstrip (we could use .lstrip)?
On Fri, Jan 24, 2014 at 03:33:48PM -0500, random832@fastmail.us wrote:
On Fri, Jan 24, 2014, at 14:17, MRAB wrote:
On 2014-01-24 16:56, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
Not necessarily:
'aaa'.replace('aa', 'x') 'xa' 'aaa'.rreplace('aa', 'x') 'ax'
Good catch!
'aaa'[::-1].replace('aa'[::-1],'x'[::-1])[::-1] 'ax'
That is very possibly the ugliest Python code I have ever seen :-) -- Steven
Ethan Furman wrote:
On 01/24/2014 09:36 AM, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace().
The funny look of the name, I think. ;)
Yes, obviously the properly serious names for them would be bytearray.evomer, tuple.xedni and list.evomer. No confusing double Rs to trip you up then. -- Greg
24.01.14 23:04, MRAB написав(ла):
On 2014-01-24 20:48, Chris Angelico wrote:
On Sat, Jan 25, 2014 at 7:33 AM,
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.
On Sat, Jan 25, 2014 at 6:16 PM, 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,
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.
Is it possible to use a reversed iterator, filter it through something that does the replacement, and then do some sort of reversed ''.join() at the end? It'd still be ugly though. ChrisA
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,
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
On Fri Jan 24 2014 at 11:55:57 PM, Georg Brandl
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 :)
Which is why you throw it in a clearly named function. def rreplace(haystack, needle, replacement, count): """Replace the N rightmost occurrences of one string with another.""" replacement.join(haystack.rsplit(needle, count))
(Er, module the missing return keyword.)
On Sat Jan 25 2014 at 12:01:28 AM, Amber Yust
On Fri Jan 24 2014 at 11:55:57 PM, Georg Brandl
wrote: 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 :)
Which is why you throw it in a clearly named function.
def rreplace(haystack, needle, replacement, count): """Replace the N rightmost occurrences of one string with another.""" replacement.join(haystack.rsplit(needle, count))
On 01/24/2014 07:20 PM, Andrew Barnert wrote:
On Jan 24, 2014, at 9:43, Ethan Furman
wrote: On 01/24/2014 09:36 AM, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: 24.01.14 18:56, Antoine Pitrou написав(ла):
On Fri, 24 Jan 2014 08:47:14 -0800 (PST) Ram Rachum
wrote: I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
I suppose it only differs when the count parameter is supplied?
I don't think it can hurt, except for the funny looks of its name. In any case, if str.rreplace is added then so should bytes.rreplace and bytearray.rreplace.
bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace().
The funny look of the name, I think. ;)
And the pronunciation. Hard to say it without sounding like a pirate. Although I guess you could interpret the rr as a rolled r: strrrrings have rrrrreplace thanks to rrrrachum.
But the inclusion of rindex makes me think this was a serious suggestion to add r versions of all methods that involve searching. Which probably isn't worth the effort to do, but there's nothing really wrong with the idea.
Those methods would better have a logical param meaning "traverse backwards", imo. D
25.01.14 09:55, Georg Brandl написав(ла):
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 :)
Indeed, it is faster if you less part of string is replaced. But the [::-1] variant looks more funny.
On 01/25/2014 07:45 AM, Alexander Heger wrote:
I propose implementing str.rreplace. (It'll be to str.replace what str.rsplit is to str.split.)
Instead of str.rreplace you could just add a parameter 'reverse=False|True' and add the same thing wherever needed, including making rfind superfluous.
This is a right way, imo, except that there is no string (/sequence) reversal here, but instead backward traversal. d
On Sat, Jan 25, 2014 at 06:57:21PM +1300, Greg Ewing
Ethan Furman wrote:
On 01/24/2014 09:36 AM, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace().
The funny look of the name, I think. ;)
Yes, obviously the properly serious names for them would be bytearray.evomer, tuple.xedni and list.evomer. No confusing double Rs to trip you up then.
While we are at it, can we also change the language a bit and add closing lines for compound operators? I suggest pairs like if/fi, for/rof and while/done. I'm still thinking about try/except/finally. That minor addition also would help to create multiline anonymous functions -- just put the body inside def/fed. (Big ugly evil grin.) Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.
Instead of str.rreplace you could just add a parameter 'reverse=False|True' and add the same thing wherever needed, including making rfind superfluous.
This is a right way, imo, except that there is no string (/sequence) reversal here, but instead backward traversal.
I suppose a better name could be found. 'traverse_backward=True|False(default)' For some of the reverse methods problems may occur if they operate on an iterator rather than an actual list, tuple, or similar.
On 01/25/2014 01:21 PM, Alexander Heger wrote:
For some of the reverse methods problems may occur if they operate on an iterator rather than an actual list, tuple, or similar.
Sure. Thus maybe the right way is to abandon this altogether and require the user to user a reverse() generator (or should i say iterator here?) instead? (this time, really reverse ;-) d
On 25/01/2014 11:15, Oleg Broytman wrote:
On Sat, Jan 25, 2014 at 06:57:21PM +1300, Greg Ewing
wrote: Ethan Furman wrote:
On 01/24/2014 09:36 AM, Antoine Pitrou wrote:
On Fri, 24 Jan 2014 19:30:00 +0200 Serhiy Storchaka
wrote: bytearray.rremove, tuple.rindex, list.rindex, list.rremove.
Not sure what those have to do with rreplace().
The funny look of the name, I think. ;)
Yes, obviously the properly serious names for them would be bytearray.evomer, tuple.xedni and list.evomer. No confusing double Rs to trip you up then.
While we are at it, can we also change the language a bit and add closing lines for compound operators? I suggest pairs like if/fi, for/rof and while/done. I'm still thinking about try/except/finally. That minor addition also would help to create multiline anonymous functions -- just put the body inside def/fed. (Big ugly evil grin.)
Oleg.
Big +1 from me. Do we toss a coin to see who gets to write the PEP? Or is it decided by the winner of yet another reenactment of the Battle of Pearl Harbour? :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
participants (17)
-
Alexander Heger
-
Amber Yust
-
Andrew Barnert
-
Antoine Pitrou
-
Chris Angelico
-
Ethan Furman
-
Georg Brandl
-
Greg Ewing
-
Mark Lawrence
-
MRAB
-
Oleg Broytman
-
Ram Rachum
-
Ram Rachum
-
random832@fastmail.us
-
Serhiy Storchaka
-
spir
-
Steven D'Aprano