Creating combination of sequences

Scott David Daniels Scott.Daniels at Acm.Org
Sat Nov 13 19:34:29 EST 2004


Minho Chae wrote:
> I'm trying to create combinations of sequences.
> 
> For example, if the sequence is 'acgt' and the length is 8,
> then I would like to have 4^8 results such as
> 'aaaaaaaa', 'aaaaaaac', 'aaaaaaag', 'aaaaaaat', ... 'tttttttt'
> 
> Is there an easy way doing this?

You are basically counting in base 4 here.

     import array

     def generate(digits='acgt', length=8):
         base = len(digits)
         positions = range(length)
         positions.reverse()
         r = array.array('c', digits[0] * length)
         for number in range(base**length):
             for digit in positions:
		number, v = divmod(number, base)
                 r[digit] = digits[v]
             yield r.tostring()

     for result in generate('ab', 2):
         print result


-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list