Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27398

How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen?

From crispy <ryniek90@gmail.com>
Newsgroups comp.lang.python
Subject How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen?
Date 2012-08-19 09:25 -0700
Organization http://groups.google.com
Message-ID <db49919e-9c9f-4e1f-8dfe-2765c6717dbe@googlegroups.com> (permalink)

Show all headers | View raw


I have an example:

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

which returns:

ATTCGT
||   |
ATCTAT
Score: 2 

GATAAATCTGGTCT
 ||  |||  |
CATTCATCATGCAA
Score: 4 

AGCG
| ||
ATCG
Score: 4 

ATCG
||||
ATCG
Score: 10


But i created this with some help from one person. Earlier, this code was devoided of these few lines:


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

The method looked liek this:

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

and produced this output:

ATTCGT
||    |
ATCTAT
Score: 2 

GATAAATCTGGTCT
| |    |     |      |         |
CATTCATCATGCAA
Score: 4 

AGCG
| |  |
ATCG
Score: 4 

ATCG
|| |  |
ATCG
Score: 10

So I have guessed, that characters processed by .rjust() function, are placed in output, relative to previous ones - NOT to first, most to left placed, character.
Why it works like that? What builtn-in function can format output, to make every character be placed as i need - relative to the first character, placed most to left side of screen.

Cheers

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 09:25 -0700
  Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 09:35 -0700
  Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? Dave Angel <d@davea.name> - 2012-08-19 13:31 -0400
    Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 12:25 -0700
    Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? crispy <ryniek90@gmail.com> - 2012-08-19 12:25 -0700
      Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? Peter Otten <__peter__@web.de> - 2012-08-20 14:45 +0200
  Re: How does .rjust() work and why it places characters relative to previous one, not to first character - placed most to left - or to left side of screen? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-19 14:56 -0400

csiph-web