substitution
Peter Otten
__peter__ at web.de
Tue Jan 19 13:49:40 EST 2010
Wyrmskull wrote:
> Peter Otten wrote:
>> def replace_many(s, pairs):
>> if len(pairs):
>> a, b = pairs[0]
>> rest = pairs[1:]
>> return b.join(replace_many(t, rest) for t in s.split(a))
>> else:
>> return s
> Proves wrong, this way x -> y -> z.
> You call replace_many again on the central part of the split
> Specifics indicate that x -> y in the end.
Sorry, I don't understand what you want to say with the above.
> Try with this:
>
> def mySubst(reps,string):
> if not(len(reps)):
> return string
> current = reps[0][0]
> a,b,c = string.partition(current)
> if b:
> return mySubst(reps,a) + reps[0][1] + mySubst (reps,c)
> else:
> return mySubst(reps[1:],string)
>
> print mySubst( ( ('foo','bar'), ('bar','qux'), ('qux','foo') ),
> 'foobarquxfoo')
>
> -------
> Wyrmskull <lordkrandel at gmail.com>
I don't see at first glance where the results of replace_many() and
mySubst() differ. Perhaps you could give an example?
Peter
PS: Please keep the conversation on-list
More information about the Python-list
mailing list