substitution

Wyrmskull lordkrandel at gmail.com
Tue Jan 19 19:56:41 CET 2010


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 



More information about the Python-list mailing list