Python, split a string into multiple substrings

I have an RNA ie line:

AUGGCCAUA

I would like to generate all the substrings as follows:

#starting from 0 character
AUG, GCC, AUA
#starting from 1 character
UGG, CCA
#starting from 2 character
GGC, CAU

I wrote code that solves the first problem:

for i in range(0,len(rna)):
  if fmod(i,3)==0:
    print rna[i:i+3]

I tried changing the starting position ie:

 for i in range(1,len(rna)):

But this leads to incorrect results:

 GCC, UA #instead of UGG, CCA

Could you give me a clue where is my mistake?

+4
source share
4 answers

The problem with your code is that you always extract the substring from the index, which is divisible by 3. Try instead

a = 'AUGGCCAUA'
def getSubStrings(RNA, position):
    return [RNA[i:i+3] for i in range(position, len(RNA) - 2, 3)]

print getSubStrings(a, 0)
print getSubStrings(a, 1)
print getSubStrings(a, 2)

Exit

['AUG', 'GCC', 'AUA']
['UGG', 'CCA']
['GGC', 'CAU']

Explanation

range(position, len(RNA) - 2, 3)will generate a list of numbers with a total difference of 3, starting from positionup to the length of the list - 2. For example,

print range(1, 8, 3)

1 - , 8 - , 3 - ,

[1, 4, 7]

. ,

[RNA[i:i+3] for i in range(position, len(RNA) - 2, 3)]
+5

, ?

for i in range(len(rna)):
    if rna[i+3:]:
        print(rna[i:i+3])

:

AUG
UGG
GGC
GCC
CCA
CAU
+2

I thought about this oneliner:

a = 'AUGGCCAUA'
[a[x:x+3] for x in range(len(a))][:-2]
+1
source
def generate(str, index):
    for i in range(index, len(str), 3):
        if len(str[i:i+3]) == 3:
            print str[i:i+3]

Example:

In [29]: generate(str, 1)
UGG
CCA

In [30]: generate(str, 0)
AUG
GCC
AUA
+1
source

All Articles