Home > Back-end >  Generating a pair of letter from a given sequence
Generating a pair of letter from a given sequence

Time:08-27

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 ?

CodePudding user response:

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']

CodePudding user response:

check this out:

string = 'ACCG'
array = []
for i in range (len(string)):
    for j in range( len(string)):
        if(string[i]   string[j] not in array):
            array.append(string[i]   string[j])
print(array)
  • Related