简体   繁体   中英

Find all permutations of a string that is variable only at specific positions in Python

I have a DNA sequence which is variable only at specific locations and need to find all possible scenarios:

DNA_seq='ANGK' #N can be T or C  and K can be A or G
N=['T','C']
K=['A','G']

Results:

['ATGA','ATGG','ACGA','ACGG']

Searching online, It seems permutations method from itertools can be used for this purpose but not sure how it can be implemented. I would appreciate any help.

Use itertools.product :

from itertools import product
result = [f'A{n}G{k}' for n, k in product(N, K)]

Result:

['ATGA', 'ATGG', 'ACGA', 'ACGG']

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