Loop from 'aaaa' to 'tttt' ?

Bengt Richter bokr at oz.net
Tue Jun 17 20:04:15 CEST 2003


On 16 Jun 2003 17:04:30 GMT, Lars Schaps <tuffi23 at gmx.de> wrote:

>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?
>
Here is another (not well tested!) alternative, in the form of an int-derived class
that can represent itself in various bases via str, and has sort of explanatory repr:

====< nbaseb.py >=========================================
class NBaseB(int):
    def __new__(cls, n, **kw):
        self = int.__new__(cls,n)
        self.base = kw.get('base',10)
        self.width = kw.get('width',1)
        self.special = 'glyphs' in kw
        self.glyphs = kw.get('glyphs','0123456789abcdefghijklmnopqrstuvwxyz')[:self.base]
        return self
    def __str__(self):
        sign = self<0 and '-' or ''
        n = abs(self)
        digs = []
        while n or self.width and len(digs)+len(sign)<self.width:
            n,d = divmod(n,self.base); digs.append(self.glyphs[d])
        digs.reverse()
        return sign+''.join(digs)
    def __repr__(self): 
        return '<NBaseB(%s) base=%s%s: %s>'%(
            int(self), self.base, ('',' (glyphs=%r)'%self.glyphs)[self.special], self)
==========================================================

 >>> from nbaseb import NBaseB
 >>> NBaseB(123) # show default repr
 <NBaseB(123) base=10: 123>
 >>> str(NBaseB(123)) # show str presentation
 '123'
 >>> for i in xrange(8): n=NBaseB(i, base=4, width=5); print '%s: %r' %(n,n)
 ...
 00000: <NBaseB(0) base=4: 00000>
 00001: <NBaseB(1) base=4: 00001>
 00002: <NBaseB(2) base=4: 00002>
 00003: <NBaseB(3) base=4: 00003>
 00010: <NBaseB(4) base=4: 00010>
 00011: <NBaseB(5) base=4: 00011>
 00012: <NBaseB(6) base=4: 00012>
 00013: <NBaseB(7) base=4: 00013>

But you wanted width 4 and special digit glyphs:

 >>> for i in xrange(8): n=NBaseB(i, base=4, width=4, glyphs='acgt'); print '%s: %r' %(n,n)
 ...
 aaaa: <NBaseB(0) base=4 (glyphs='acgt'): aaaa>
 aaac: <NBaseB(1) base=4 (glyphs='acgt'): aaac>
 aaag: <NBaseB(2) base=4 (glyphs='acgt'): aaag>
 aaat: <NBaseB(3) base=4 (glyphs='acgt'): aaat>
 aaca: <NBaseB(4) base=4 (glyphs='acgt'): aaca>
 aacc: <NBaseB(5) base=4 (glyphs='acgt'): aacc>
 aacg: <NBaseB(6) base=4 (glyphs='acgt'): aacg>
 aact: <NBaseB(7) base=4 (glyphs='acgt'): aact>

 >>> [str(NBaseB(i, base=4, width=4, glyphs='acgt')) for i in xrange(256)]
 ['aaaa', 'aaac', 'aaag', 'aaat', 'aaca', 'aacc', 'aacg', 'aact', 'aaga', 'aagc', 'aagg', 'aagt',
  'aata', 'aatc', 'aatg', 'aatt', 'acaa', 'acac', 'acag', 'acat', 'acca', 'accc', 'accg', 'acct',
  'acga', 'acgc', 'acgg', 'acgt', 'acta', 'actc', 'actg', 'actt', 'agaa', 'agac', 'agag', 'agat',
  'agca', 'agcc', 'agcg', 'agct', 'agga', 'aggc', 'aggg', 'aggt', 'agta', 'agtc', 'agtg', 'agtt',
  'ataa', 'atac', 'atag', 'atat', 'atca', 'atcc', 'atcg', 'atct', 'atga', 'atgc', 'atgg', 'atgt',
  'atta', 'attc', 'attg', 'attt', 'caaa', 'caac', 'caag', 'caat', 'caca', 'cacc', 'cacg', 'cact',
  'caga', 'cagc', 'cagg', 'cagt', 'cata', 'catc', 'catg', 'catt', 'ccaa', 'ccac', 'ccag', 'ccat',
  'ccca', 'cccc', 'cccg', 'ccct', 'ccga', 'ccgc', 'ccgg', 'ccgt', 'ccta', 'cctc', 'cctg', 'cctt',
  'cgaa', 'cgac', 'cgag', 'cgat', 'cgca', 'cgcc', 'cgcg', 'cgct', 'cgga', 'cggc', 'cggg', 'cggt',
  'cgta', 'cgtc', 'cgtg', 'cgtt', 'ctaa', 'ctac', 'ctag', 'ctat', 'ctca', 'ctcc', 'ctcg', 'ctct',
  'ctga', 'ctgc', 'ctgg', 'ctgt', 'ctta', 'cttc', 'cttg', 'cttt', 'gaaa', 'gaac', 'gaag', 'gaat',
  'gaca', 'gacc', 'gacg', 'gact', 'gaga', 'gagc', 'gagg', 'gagt', 'gata', 'gatc', 'gatg', 'gatt',
  'gcaa', 'gcac', 'gcag', 'gcat', 'gcca', 'gccc', 'gccg', 'gcct', 'gcga', 'gcgc', 'gcgg', 'gcgt',
  'gcta', 'gctc', 'gctg', 'gctt', 'ggaa', 'ggac', 'ggag', 'ggat', 'ggca', 'ggcc', 'ggcg', 'ggct',
  'ggga', 'gggc', 'gggg', 'gggt', 'ggta', 'ggtc', 'ggtg', 'ggtt', 'gtaa', 'gtac', 'gtag', 'gtat',
  'gtca', 'gtcc', 'gtcg', 'gtct', 'gtga', 'gtgc', 'gtgg', 'gtgt', 'gtta', 'gttc', 'gttg', 'gttt',
  'taaa', 'taac', 'taag', 'taat', 'taca', 'tacc', 'tacg', 'tact', 'taga', 'tagc', 'tagg', 'tagt',
  'tata', 'tatc', 'tatg', 'tatt', 'tcaa', 'tcac', 'tcag', 'tcat', 'tcca', 'tccc', 'tccg', 'tcct',
  'tcga', 'tcgc', 'tcgg', 'tcgt', 'tcta', 'tctc', 'tctg', 'tctt', 'tgaa', 'tgac', 'tgag', 'tgat',
  'tgca', 'tgcc', 'tgcg', 'tgct', 'tgga', 'tggc', 'tggg', 'tggt', 'tgta', 'tgtc', 'tgtg', 'tgtt',
  'ttaa', 'ttac', 'ttag', 'ttat', 'ttca', 'ttcc', 'ttcg', 'ttct', 'ttga', 'ttgc', 'ttgg', 'ttgt',
  'ttta', 'tttc', 'tttg', 'tttt']

Regards,
Bengt Richter




More information about the Python-list mailing list