2

所以,我有这个代码:

def pairwiseScore(seqA, seqB):

    score = 0
    length = len(seqA)
    similarity = []

    for x in xrange(length):

        if seqA[x] == seqB[x]:
            if (x >= 1) and (seqA[x - 1] == seqB[x - 1]):
                score += 3
                similarity.append(x)
            else:
                score += 1
                similarity.append(x)                
        else:
            score -= 1

    return ''.join((seqA, '\n', ''.join(['|'.rjust(x) for x in similarity]), '\n', seqB, '\n', 'Score: ', str(score)))

这是本练习的解决方案。

它几乎可以正常工作,但是,当我执行时:

print pairwiseScore("ATTCGT", "ATCTAT"), '\n', '\n', pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA")

我得到这个输出:

ATTCGT
||    |
ATCTAT
Score: 2 

GATAAATCTGGTCT
| |    |     |      |         |
CATTCATCATGCAA
Score: 4

如您所见,这些管道(或垂直条)的格式不正确。

它应该如下所示:

>>> print pairwiseScore("ATTCGT", "ATCTAT")
ATTCGT
||   |
ATCTAT
Score: 2 
>>> print pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA")
GATAAATCTGGTCT
||  |||  |   
CATTCATCATGCAA
Score: 4 
>>>

我的问题是:

这有什么问题:

''.join(['|'.rjust(x) for x in similarity]

功能?我必须如何编辑它,以使这些管道在输出时格式化良好?干杯!

4

2 回答 2

1

您的相似度值是条形的绝对位置,但是您使用 rjust() 函数的方式,它应该传递相对于前一个位置的位置。

例如,您可以在函数的底部执行此操作:

prev = -1
relative_similarity=[]
for x in similarity:
  relative_similarity.append(x-prev)
  prev=x

return ''.join((seqA, '\n', ''.join(['|'.rjust(x) for x in relative_similarity]), '\n', seqB, '\n', 'Score: ', str(score)))
于 2012-08-18T15:10:18.857 回答
1

所以,多亏了Vaughn Cato,我找到了解决方案。使用您的几行代码,我的方法终于奏效了,但没有通过最后一个测试用例->在此处输入图像描述

那是代码:

def pairwiseScore(seqA, seqB):

    prev = -1
    score = 0
    length = len(seqA)
    similarity = []
    relative_similarity = []

    for x in xrange(length):

        if seqA[x] == seqB[x]:
            if (x >= 1) and (seqA[x - 1] == seqB[x - 1]):
                score += 3
                similarity.append(x)
            else:
                score += 1
                similarity.append(x)                
        else:
            score -= 1

    for x in similarity:

        relative_similarity.append(x - prev)
        prev = x

    return ''.join((seqA, '\n', ''.join(['|'.rjust(x) for x in relative_similarity]), '\n', seqB, '\n', 'Score: ', str(score)))

所以我修改了你的例子,并做了这个:

def pairwiseScore(seqA, seqB):

    score = 0
    bars = [str(' ') for x in seqA] #create a list filled with number of spaces equal to length of seqA string. It could be also seqB, because both are meant to have same length
    length = len(seqA)
    similarity = []

    for x in xrange(length):

        if seqA[x] == seqB[x]: #check if for every index 'x', corresponding character is same in both seqA and seqB strings
            if (x >= 1) and (seqA[x - 1] == seqB[x - 1]): #if 'x' is greater than or equal to 1 and characters under the previous index, were same in both seqA and seqB strings, do..
                score += 3
                similarity.append(x)
            else:
                score += 1
                similarity.append(x)                
        else:
            score -= 1

    for x in similarity:
        bars[x] = '|' #for every index 'x' in 'bars' list, replace space with '|' (pipe/vertical bar) character 

    return ''.join((seqA, '\n', ''.join(bars), '\n', seqB, '\n', 'Score: ', str(score)))

并且此代码通过了 s3-q11 练习中的所有测试用例。Sooo,我想我得到了解决方案,我已经完成了。

谢谢和欢呼:)

于 2012-08-19T19:21:58.210 回答