1

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!

AcK
  • 2,063
  • 2
  • 20
  • 27
Sofia
  • 365
  • 3
  • 17

4 Answers4

1

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)]
ap1997
  • 183
  • 6
0

Simple indexing will do the job

s = 'ACGT'
[s[i:i+2] for i in range(len(s)-1)]
['AC', 'CG', 'GT']
Epsi95
  • 8,832
  • 1
  • 16
  • 34
0

Try:

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

li:

['AC', 'CG', 'GT']
Pygirl
  • 12,969
  • 5
  • 30
  • 43
0

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