如何优化if语句?

时间:2019-12-02 15:59:12

标签: python python-3.x if-statement optimization

我有一部分代码要优化。 如果值不是我想要的值,我使用if语句跳过分析。 为了帮助理解,在我的数据中,R1在range(6,15)之间;范围错误(0,1);间隔(0,10)。

我的代码正在运行,但是如果能根据我拥有的数据量更快地运行,那会更好。

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
elif (R1 == 6 and mis == 0 and spacer in range(0,4)) :
    pass
elif (R1 == 7 and mis == 0 and spacer in range(0,2)) :
    pass
elif (R1 == 7 and mis == 1 and spacer in range(0,4)) :
    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')

0 个答案:

没有答案