lundi 2 décembre 2019

How to optimize if statement in python 3

I am a beginner on python. I have a part of my code I want to optimize. I use if statement to skip the analyze if the values are not the one I want. Just to help the comprehension, in my data, R1 is between range(6,15); mis in range(0,1); spacer in range (0,10).

My code is working but it would be better if it will run faster according to the amount of data I have.

Thank you in advance for your help !

R1 = len(hit_splitted_line1[1])
hit = re.sub(r"\s+", "", lines[i+1])
hit_nb = len(hit)
spacer = pos2_spacer - pos1_spacer - 1

mis = R1 - hit_nb
if (R1 == 6 and mis ==1):
    pass
if (R1 == 6 and mis == 0 and spacer in range(0,3)) :
    pass
if (R1 == 7 and mis == 0 and spacer in range(0,1)) :
    pass
if (R1 == 7 and mis == 1 and spacer in range(0,3)) :
    pass
else :
    if (R1 == 8 and spacer in range(0,1)):
        goodAln = re.search('^\s*\|\|\|\|\|\|', lines[i+1])
        if (not goodAln):
            pass
        else :
            name_list=[str(chr),str(pos_start),str(pos_end)]
            name = '_'.join(name_list)
            ID = "ID="+name+";spacer="+str(spacer)+";repeat="+str(R1)+";mismatch="+str(mis)
            print (chr,"pal2gff","IR",pos_start,pos_end,".","+",".",str(ID),sep='\t')
    else :
        name_list=[str(chr),str(pos_start),str(pos_end)]
        name = '_'.join(name_list)
        ID = "ID="+name+";spacer="+str(spacer)+";repeat="+str(R1)+";mismatch="+str(mis)
        print (chr,"pal2gff","IR",pos_start,pos_end,".","+",".",str(ID),sep='\t')

Aucun commentaire:

Enregistrer un commentaire