[Tutor] list.replace -- string.swap
A.T.Hofkamp
a.t.hofkamp at tue.nl
Tue Mar 17 12:45:23 CET 2009
Hello Denis,
spir wrote:
> Is there a list.replace builtin I cannot find? Or a workaround?
You didn't find it because it does not exist.
You should do something like
[f(x) for x in old_lst]
where f(x) implements the replacement.
> Also: How would perform string.swap(s1, s2) in the following cases:
>
> * There is no secure 'temp' char, meaning that
> s.replace(s1,temp).replace(s2,s1).replace(temp,s2)
> will fail because any char can be part of s.
split on s1, replace all pieces s2 -> s1, join with s2 ?
pieces = txt.split(s1)
pieces = [p.replace(s2, s1) for p in pieces]
text = s2.join(pieces)
which may not work if s1 and s2 overlap, but that case was not described above.
Sincerely,
Albert
More information about the Tutor
mailing list