在列表推导中使用.rjust()进行字符串对齐 - 格式化工作与预期不同

时间:2012-08-18 15:04:46

标签: python string list formatting alignment

所以,我有这个代码:

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)))

这是this exercise的解决方案。

它的效果差不多,但是,当我执行时:

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]

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

2 个答案:

答案 0 :(得分: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)))

答案 1 :(得分:1)

所以,感谢Vaughn Cato,我找到了解决方案。使用几行代码,我的方法终于奏效了,但没有通过最后一个测试用例 - > enter image description here

这就是代码:

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,我想我得到了解决方案而且我已经完成了。

谢谢和欢呼:)