shijunxiao的个人博客分享 http://blog.sciencenet.cn/u/shijunxiao

博文

if语句陷阱

已有 1882 次阅读 2017-5-21 11:21 |个人分类:python|系统分类:科研笔记

简单的一个if语句的排列,seq运行截然不同的结果,程序需要逻辑:

程序段1:

       fIn=open(fastaFile, 'r')
       for line in fIn:
               lineNum += 1
               if lineNum == 1:
                       tmp = line.replace('n', '').split('/')
           if len(tmp) == 3:
               if tmp[2].split('_')[0] == "0":
                   length = int(tmp[2].split('_')[1])
               else:
                   print "begin coord in seq header is wrong for pacbio fasta file."
                   return 0
           else:
               print "seq header format is wrong for pacbio fasta file."
               return 0

       if line[0] == '>' and lineNum > 1:
               print length, len(seq)
               if length == len(seq):
                   return 1
               else:
                   print "end coord in seq header is wrong for pacbio fasta file."
                   return 0
       else:
           seq += line.replace('n', '')
       fIn.close()

程序段2:

       fIn=open(fastaFile, 'r')
       for line in fIn:
               lineNum += 1
               if lineNum == 1:
                       tmp = line.replace('n', '').split('/')
           if len(tmp) == 3:
               if tmp[2].split('_')[0] == "0":
                   length = int(tmp[2].split('_')[1])
               else:
                   print "begin coord in seq header is wrong for pacbio fasta file."
                   return 0
           else:
               print "seq header format is wrong for pacbio fasta file."
               return 0

       if line[0] == '>':
           if lineNum > 1:

               print length, len(seq)
               if length == len(seq):
                   return 1
               else:
                   print "end coord in seq header is wrong for pacbio fasta file."
                   return 0
       else:
           seq += line.replace('n', '')
       fIn.close()




https://blog.sciencenet.cn/blog-2890242-1056167.html

上一篇:使用python实现range merge
下一篇:武汉新型冠状病毒事件亲历有感
收藏 IP: 113.57.171.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 05:25

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部