Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generating a pair of letter from a given sequence

I have a problem to be solved and I would appreciate if anyone can help. I want to generate all possible two-letters string from the given sequence. For example from string ‘ACCG’, I want to generate a list of [AA, CC, GG, AC,CA,AG,GA,CG,GC].

Does anyone have an idea how I can do that ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

An efficient solution can be coded using itertools module

CODE

import itertools

string = 'ACCG'
num = 2

combinations = list(itertools.product(string, repeat=num))
result = [*set([''.join(tup) for tup in combinations])]

print(result)

OUTPUT

['CG', 'GG', 'GC', 'GA', 'AG', 'AA', 'CC', 'AC', 'CA']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading