简体   繁体   中英

Generate permutations of a list of lists with variable lengths

I am looking to generate all of the possible combinations of characters where each position in the list of characters may or may not have multiple choices.

For example, if I have the list [['A','G'],['A','C','G'],'T'] I would like to generate from it ['AAT','ACT','AGT','GAT','GCT','GGT'] . What is the best way to go about this? I have used itertools in the past but I don't see how any of the functions can handle this sort of request.

Use itertools.product() :

from itertools import product

lsts = [['A', 'G'], ['A', 'C', 'G'], 'T']
output = [''.join(combo) for combo in product(*lsts)]

The *lsts syntax applies each element in lsts as a separate argument to the products() function; as if you called product(['A', 'G'], ['A', 'C', 'G'], 'T') .

Demo:

>>> from itertools import product
>>> lsts = [['A','G'],['A','C','G'],'T']
>>> [''.join(combo) for combo in product(*lsts)]
['AAT', 'ACT', 'AGT', 'GAT', 'GCT', 'GGT']

You could reduce your nested lists to strings for the same output:

lsts = ['AG', 'ACG','T']

or, for consistency's sake, make the last element a list:

lsts = [['A', 'G'], ['A', 'C', 'G'], ['T']]

but it'll work with the mixed sequences too.

First, you should make your list homogeneous (list of lists) by using ['T'] instead of 'T'

>>> import itertools
>>> L = [['A','G'],['A','C','G'],['T']]
>>> [''.join(x) for x in itertools.product(*L)]
['AAT', 'ACT', 'AGT', 'GAT', 'GCT', 'GGT']

Iterating the string 'T' does also work, but it's likely to cause other bugs in my experience

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM