Iterativelly combine strings and remove the first strings

Viewed 46

For a given string I want to make groups two by two, but when the first group is created I want to delete the first letter and then group those. Like this: Initial string: 'ACGT' Goal: ['AC','CG','GT']

So I did this code:

for i in range(0, len(seq_example), n):
    out = [seq_example[i:i + n(len(seq_example) - 1)]]
    

However, there is one missing combination. Can somemone help me please!

4 Answers

I believe you can do:

seq_example = "ACGT"
window = 2
out = [seq_example[i:i+window] for i in range(len(seq_example)-window + 1)]

Simple indexing will do the job

s = 'ACGT'
[s[i:i+2] for i in range(len(s)-1)]
['AC', 'CG', 'GT']

Try:

seq_example = list('ACGT')
li = list(map("".join, zip(seq_example, seq_example[1:])))

li:

['AC', 'CG', 'GT']

I found a solution to your problem in Python.

I Used your code and change some things and I think that now it should work well.

seq_example ='ACGT'

out =[]
for i in range(len(seq_example)-1):
    out.append(seq_example[i:i+2])

print(out)

enter image description here

Related