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?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
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 Sun, 19 Aug 2012 09:25:03 -0700 (PDT)
Organization http://groups.google.com
Lines 115
Message-ID <db49919e-9c9f-4e1f-8dfe-2765c6717dbe@googlegroups.com> (permalink)
NNTP-Posting-Host 89.231.125.162
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
X-Trace posting.google.com 1345393854 2719 127.0.0.1 (19 Aug 2012 16:30:54 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Sun, 19 Aug 2012 16:30:54 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=89.231.125.162; posting-account=igkU0woAAAC8IxhDwc4Ves5KfH9PcQQY
User-Agent G2/1.0
Xref csiph.com comp.lang.python:27398

Show key headers only | 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