[Tutor] substitute function
Alan G
alan.gauld at freenet.co.uk
Mon Aug 1 00:07:01 CEST 2005
> Is there a 'substitute' function in python.
Assuming we are dealing with strings then there are two.
The string objects provide a replace() method and the
regulatr expression module re provides substitute.
> I want to substitute A with T and G with C and vice
> versa
>
> A -> T
> G -> C
> T -> A
> c -> G
But the vice-versa creates an awkward sequencing problem.
Whichever one you do first messes you up for the reverse
mapping. Youi need to do multiple swaps at once.
ISTR a module being highlighted in the Python challenge
thing that was muxch discussed a few weeks ago that might
help here, otherwise I'd create a map table and just brute
force scan each character and replace where the character
is in the table.
--------- untested pseudo code --------
swaps = {'A':'T', 'G':'C',...}
chars = list(longstring)
for index in len(chars):
if chars[index] in swaps:
chars[index] = swaps[chars[index]]
longstring = ''.join(chars)
Not very fast but it would do it. If the strings are very
long then I might try some clever regex stuff, or look up
the old thread to find that clever module..
HTH,
Alan G.
More information about the Tutor
mailing list