Hotaru Hotaru - 1 month ago 15
Python Question

incrementing by 1 for a row and column for a slice

Hi am a newbie with python and have tried to write the program below:

import numpy as np #import package for scientific computing
dna1 = str(np.load('dna1.npy'))
def count(dna1, repeat):
i = 0
for s in range(len(dna1)):
if (s =='repeat'):
i += 1
s += dna1[0:1]
return i
repeat = 'TTTT'
n = count(dna1, repeat)
print ('{repeat} occurs {n} times in dna1'.format(repeat=repeat, n=n))


I want to extract every possible combination of 4 letters in a list and check whether they are equal to
'TTTT'
. But I do not know how to increment to make s shift by 1 position in my list but still read 4 letters.

Answer

I agree that trying to use a regex is probably the easiest initial approach:

import numpy as np    #import package for scientific computing 
import re

dna1 = str(np.load('dna1.npy'))    

def count(dna1, repeat):
    regex = re.compile(repeat)
    result = regex.findall(dna1)
    return len(result)

repeat = 'TTTT'
n = count(dna1, repeat)
print ('{repeat} occurs {n} times in dna1'.format(repeat=repeat, n=n))
Comments