.rjust() 是如何工作的,为什么它将字符相对于前一个字符而不是第一个字符 - 放置在最左侧 - 或屏幕左侧?
我有一个例子:
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)))
print pairwiseScore("ATTCGT", "ATCTAT"), '\n', '\n', pairwiseScore("GATAAATCTGGTCT", "CATTCATCATGCAA"), '\n', '\n', pairwiseScore('AGCG', 'ATCG'), '\n', '\n', pairwiseScore('ATCG', 'ATCG')
返回:
ATTCGT
|| |
ATCTAT
Score: 2
GATAAATCTGGTCT
|| ||| |
CATTCATCATGCAA
Score: 4
AGCG
| ||
ATCG
Score: 4
ATCG
||||
ATCG
Score: 10
但我在一个人的帮助下创建了这个。早些时候,这段代码没有这几行:
prev = -1
relative_similarity = []
for x in similarity:
relative_similarity.append(x - prev)
prev = x
该方法如下所示:
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)))
并产生了这个输出:
ATTCGT
|| |
ATCTAT
Score: 2
GATAAATCTGGTCT
| | | | | |
CATTCATCATGCAA
Score: 4
AGCG
| | |
ATCG
Score: 4
ATCG
|| | |
ATCG
Score: 10
所以我猜到,.rjust()
函数处理的字符被放置在输出中,相对于以前的字符 - 不是第一个,最左边的字符。
为什么它会这样工作?什么内置函数可以格式化输出,使每个字符都按我的需要放置 - 相对于第一个字符,放置在屏幕的最左侧。