Loop from 'aaaa' to 'tttt' ?
Andrew Walkingshaw
andrew-usenet at lexical.org.uk
Mon Jun 16 13:36:59 EDT 2003
In article <3EEDFD89.CD2677D8 at engcorp.com>, Peter Hansen wrote:
>
>>>> set = 'acgt'
>>>> sets = [''.join(a,b,c,d) for a in set for b in set for c in set for d in set]
>>>> sets
> ['aaaa', 'aaac', 'aaag', 'aaat', 'aaca', 'aacc', 'aacg', 'aact', 'aaga', 'aagc',
> 'aagg', 'aagt', 'aata', 'aatc', 'aatg', 'aatt', 'acaa', 'acac', 'acag', 'acat',
> ...[snip]...
> 'ttgg', 'ttgt', 'ttta', 'tttc', 'tttg', 'tttt']
>
>:-)
Lovely! I knew there must be a list-comprehension based answer. There
was a similar thread back in 2001, which contained the following answers
to the related problem of "find the combinations of the members of n
variable-length lists":
def CartesianProduct(alist):
res = []
if len(alist) < 2:
for x in alist[0]:
res.append([x])
else:
tails = CartesianProduct(alist[1:])
for x in alist[0]:
for t in tails:
res.append([x]+t)
return res
print CartesianProduct(["a","c","g","t" for x in range(4)])
being David Ullrich's recursive solution. My non-recursive solution was
deliberately contrary at the time, and hasn't aged well :)
Anyway, the OP appears to be doing bioinformatics, so the general case
might be useful to him later...
- Andrew
--
Andrew Walkingshaw | andrew-usenet at lexical.org.uk
More information about the Python-list
mailing list