[ python-Feature Requests-1184678 ] "replace" function should accept lists.

SourceForge.net noreply at sourceforge.net
Sat Apr 30 11:48:54 CEST 2005


Feature Requests item #1184678, was opened at 2005-04-17 19:05
Message generated for change (Comment added) made by poromenos
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1184678&group_id=5470

Category: None
Group: None
Status: Open
Resolution: Rejected
Priority: 5
Submitted By: Poromenos (poromenos)
Assigned to: Nobody/Anonymous (nobody)
Summary: "replace" function should accept lists.

Initial Comment:
It would be nice if the "replace" function accepted lists/
tuples and replaced each item in the "old" list with the 
corresponding item in the "new" list.

----------------------------------------------------------------------

>Comment By: Poromenos (poromenos)
Date: 2005-04-30 12:48

Message:
Logged In: YES 
user_id=267121

That is true, the alternative loop is quite usable, but the API 
change would be backwards-compatible, and the 
implementation is not very difficult... I just see this as a nice 
feature of replace, it's not really necessary if it'll break other 
stuff.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-04-29 17:10

Message:
Logged In: YES 
user_id=80475

Given the multiple alternative input matches, this is a job
for regular expressions.  See the string.substitute() source
code for an approach to making the transformation you outlined.

I do not think multi-replace is sufficiently common to
warrant a change to the API.  If needed and if the regexp
solution is too hard, then a regular for-loop is a
reasonable alternative:

    for old, new in zip(oldlist, newlist):
         s = s.replace(old, new)

----------------------------------------------------------------------

Comment By: Poromenos (poromenos)
Date: 2005-04-29 16:03

Message:
Logged In: YES 
user_id=267121

There was an oversight on my part... Translate can only be 
used to change individual characters, what I am proposing 
could replace strings of multiple characters, essentially 
concatenating multiple replace()s in one:
>>> "h.w".replace(["h", ".", "w"], ["Hello", " ", "World!"])
'Hello, World!'

----------------------------------------------------------------------

Comment By: Poromenos (poromenos)
Date: 2005-04-19 01:23

Message:
Logged In: YES 
user_id=267121

Ah, I did not know that... The docs are a bit complicated on .
translate, but since it can do that, yes, it would be unwise to 
implement my suggestion.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-04-19 01:20

Message:
Logged In: YES 
user_id=80475

I'm -1 on complicating str.replace()'s API for functionality
that substantially duplicates that offered by str.translate():

>>> "Hello world".translate(string.maketrans('ed', 'ae'))
'Hallo worle'

----------------------------------------------------------------------

Comment By: Poromenos (poromenos)
Date: 2005-04-19 00:15

Message:
Logged In: YES 
user_id=267121

Hmm, actually I was requesting that string.replace() accepted 
lists of substitutions, like so:
>>> "Hello world".replace(["e", "d"], ["a", "e"])
'Hallo worle'

But your suggestion would also be very useful.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-04-19 00:11

Message:
Logged In: YES 
user_id=80475

Are you requesting that lists be given a replace() method
that parallels the replace() method for strings?

def replace(self, old, new):
    result = []
    for elem in self:
        if elem == old:
            result.append(new)
        else:
            result.append(elem)
    self[:] = result

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=1184678&group_id=5470


More information about the Python-bugs-list mailing list