python条件并计算总和?

时间:2011-08-26 10:18:39

标签: python

根据以下评分方案编写一个比较两个DNA序列的函数:匹配+1,每个连续匹配+3,每个不匹配-1。

我的代码:

def pairwiseScore(seqA, seqB): 
  '''1+1+3-1-1+1+3+1-1-1+1-1-1-1'''
  print 
  signed = ''
  score = 0
  for i in range(len(seqA)):
    if(seqA[i] == seqB[i]):         
        signed += '|'
        score += 1
        if signed=='||':
            score=0
            score += 3
        else:
            score += 1

    else:
        signed += ' '
        score -=1
  return seqA+"\n"+signed+"\n"+seqB+"\n"+'score:'+ str(score)

print pairwiseScore("ATTCGT", "ATCTAT")  
print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA")
print pairwiseScore('ATCG', 'ATCG')
print pairwiseScore('CATTCATCATGCAA', 'GATAAATCTGGTCT')

OUPUT:

ATTCGT
||   |
ATCTAT
score:2

GATAAATCTGGTCT
 ||  |||  |   
CATTCATCATGCAA
score:4

ATCG
||||
ATCG
score:7  // this should be 10 because +3 for each consecutive match` 1 + 3 +3 +3 =10

CATTCATCATGCAA
 ||  |||  |   
GATAAATCTGGTCT
score:4

有人能给我提示吗?

感谢

2 个答案:

答案 0 :(得分:1)

if signed=='||':

此代码仅在signed完全等于'||'时才匹配。

你想看看最后两个字符,所以试试:

if signed[-2:] == '||':

我认为你在分数计算中也有计算错误。

这是您的功能的固定和清理版本:

def pairwiseScore(seqA, seqB): 
    signed = ''
    score = 0
    for i in range(len(seqA)):
      if seqA[i] == seqB[i]:
          signed += '|'
          if signed[-2:] == '||':
              score += 3
          else:
              score += 1
      else:
          signed += ' '
          score -= 1
    return '%s\n%s\n%s\nscore:%d' % (seqA, signed, seqB, score)

答案 1 :(得分:1)

Look here

def pairwiseScore(seqA, seqB): 
  '''1+1+3-1-1+1+3+1-1-1+1-1-1-1'''
  print 
  signed = ''
  score = 0
  for i in range(len(seqA)):
    if(seqA[i] == seqB[i]):         
        signed += '|'
        if i > 0 and signed[len(signed)-2]=='|':
            score += 3
        else:
            score += 1

    else:
        signed += ' '
        score -=1
  return seqA+"\n"+signed+"\n"+seqB+"\n"+'score:'+ str(score)

print pairwiseScore("ATTCGT", "ATCTAT")  
print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA")
print pairwiseScore('ATCG', 'ATCG')
print pairwiseScore('CATTCATCATGCAA', 'GATAAATCTGGTCT')