Loop from 'aaaa' to 'tttt' ?
Des Small
des.small at bristol.ac.uk
Mon Jun 16 13:42:40 EDT 2003
Lars Schaps <tuffi23 at gmx.de> writes:
> Hello.
>
> In my program in need a loop from 'aaaa' over
> 'aaac', 'aaag', 'aaat', 'aaca' to 'tttt'.
> (Possible characters 'a', 'c', 'g' and 't')
>
> One idea i had is to take a number n with the base of
> 4 and use
>
> t= string.translate( '0123', 'acgt')
> string.translate( n, t)
>
> But i don't know how to convert from base10 to base4.
>
> Has anyone a idea?
I like to use recursive generators for this sort of thing:
from __future__ import generators # Not needed in python 2.3
def base_iter():
for a in 'acgt': yield a
def nbase_iter(n):
if n==0: yield ''
else:
for first in base_iter():
for rest in nbase_iter(n-1):
yield first + rest
After which you can do:
>>> list(nbase_iter(4))[:5]
['aaaa', 'aaac', 'aaag', 'aaat', 'aaca']
>>> list(nbase_iter(4))[-5:]
['ttgt', 'ttta', 'tttc', 'tttg', 'tttt']
Des
always wanted to do some bioinformatics.
--
Des Small / Scientific Programmer/ School of Mathematics /
University of Bristol / UK / Word falling / Image falling
More information about the Python-list
mailing list