Re: substitution
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. Your flowing pythonicity (if len(x):) gave me lots of inspiration. I bet this will win the prize ;) - def mySubst(reps,string): if not(len(reps)): return string a,b,c = string.partition(reps[0][0]) 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: substitution
Cleaned. You can remove the 'else's if you want, because 'return' breaks the instruction flow. Should also work with other sequence types. def mySubst(reps,seq): if reps: a,b,c = string.partition(reps[0][0]) if b: return mySubst(reps,a) + reps[0][1] + mySubst (reps,c) else: return mySubst(reps[1:], seq) else: return seq print mySubst( ( ('foo','bar'), ('bar','qux'), ('qux','foo') ), 'foobarquxfoo') print mySubst( ( ('foo','bar'), ('bar','qux'), ('qux','foo') ), 'foobarquxxxfoo') --- Wyrmskull -- http://mail.python.org/mailman/listinfo/python-list
Re: substitution
Nvm, my bad, I misunderstood the split instruction. No difference :) --- Wyrmskull P.S Sorry about the P.M., I misclicked on a GUI -- http://mail.python.org/mailman/listinfo/python-list