[Tutor] Fw: list.replace -- string.swap
ALAN GAULD
alan.gauld at btinternet.com
Wed Mar 18 09:45:14 CET 2009
Forwarding to list
> From: spir <denis.spir at free.fr>
> To: Alan Gauld <alan.gauld at btinternet.com>
> Sent: Wednesday, 18 March, 2009 8:41:39 AM
> Subject: Re: [Tutor] list.replace -- string.swap
>
> Le Tue, 17 Mar 2009 20:46:06 -0000,
> "Alan Gauld" s'exprima ainsi:
> > "spir" wrote
> Sorry, I was unclear all along my post.
>
> > > Is there a list.replace builtin I cannot find? Or a workaround?
> >
> > myList[x] = newValue
> >
> > Or is that too obvious?
>
> I mean like string.replace. Something like:
>
> def replace(seq, v1, v2):
> for (index,item) in enumerate(seq):
> if item == v1:
> seq[index] = v2
>
> Now, I know i does not exist.
>
> > > Also: How would perform string.swap(s1, s2) in the following cases:
>
> Here I mean exchanging s1 and s2 occurrences all along a string. As an example,
> in a text containing numerous formatted numbers, switch from english to european
> format:
> 1,234,567.89
> 1.234.567,89
> meaning exchange '.' and ','.
>
> > > * 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.
>
> The use of a temp char for marking places of one the chars to be swapped is a
> common trick. But if the text can contain any char (even chr(0)), then there no
> char you can safely use as temp.
> The only workaround I know is to pass through lists. Then swap on the list,
> using eg None as temp item, and glue back the result to a string. But you need
> them a replace method on lists, hence my previous question ;-)
>
> > > * Either s1 or s2 can be more than a single char.
>
> More difficult, cause you cannot simple list() the string. It must split on s1
> and s2, but keeping the delimiters! There is no option afaik for that in
> string.split -- too bad! So that you must split it manually at start and end of
> each instance of s1 and s2. Or there are other algorithms I cannot figure out.
> What I was asking for.
>
> > Sorry you lost me there. No idea what you mean.
>
>
> Sorry, me!
>
> > Alan G.
>
> denis
> ------
> la vita e estrany
More information about the Tutor
mailing list