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


Groups > comp.lang.python > #27398 > unrolled 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?

Started bycrispy <ryniek90@gmail.com>
First post2012-08-19 09:25 -0700
Last post2012-08-19 14:56 -0400
Articles 7 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#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?

Fromcrispy <ryniek90@gmail.com>
Date2012-08-19 09:25 -0700
SubjectHow 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?
Message-ID<db49919e-9c9f-4e1f-8dfe-2765c6717dbe@googlegroups.com>
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

[toc] | [next] | [standalone]


#27400

Fromcrispy <ryniek90@gmail.com>
Date2012-08-19 09:35 -0700
Message-ID<a7c8f6c8-e0e4-4e51-a0d2-103b591c49ea@googlegroups.com>
In reply to#27398
Here's first code -> http://codepad.org/RcKTTiYa

And here's second -> http://codepad.org/zwEQKKeV

[toc] | [prev] | [next] | [standalone]


#27402

FromDave Angel <d@davea.name>
Date2012-08-19 13:31 -0400
Message-ID<mailman.3510.1345397520.4697.python-list@python.org>
In reply to#27398
On 08/19/2012 12:25 PM, crispy wrote:
> <SNIP>
> 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.

rjust() does not print to the console, it just produces a string.  So if
you want to know how it works, you need to either read about it, or
experiment with it.

Try   help("".rjust)     to see a simple description of it.  (If you're
not familiar with the interactive interpreter's help() function, you owe
it to yourself to learn it).

Playing with it:

print "abcd".rjust(8, "-")       produces    ----abcd

for i in range(5): print "a".rjust(i, "-")
    produces:

a
a
-a
--a
---a

In each case, the number of characters produced is no larger than i.  No
consideration is made to other strings outside of the literal passed
into the method.


> Why it works like that? 

In your code, you have the rjust() method inside a loop, inside a join,
inside a print.  it makes a nice, impressive single line, but clearly
you don't completely understand what the pieces are, nor how they work
together.  Since the join is combining (concatenating) strings that are
each being produced by rjust(), it's the join() that's making this look
"relative" to you.


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

If you want to randomly place characters on the screen, you either want
a curses-like package, or a gui.  i suspect that's not at all what you want.

if you want to randomly change characters in a pre-existing string,
which will then be printed to the console, then I could suggest an
approach (untested)

res = [" "] * length
for column in similarity:
    res[column] = "|"
res = "".join(res)



-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#27424

Fromcrispy <ryniek90@gmail.com>
Date2012-08-19 12:25 -0700
Message-ID<mailman.3524.1345404343.4697.python-list@python.org>
In reply to#27402
W dniu niedziela, 19 sierpnia 2012 19:31:30 UTC+2 użytkownik Dave Angel napisał:
> On 08/19/2012 12:25 PM, crispy wrote:
> 
> > <SNIP>
> 
> > 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.
> 
> 
> 
> rjust() does not print to the console, it just produces a string.  So if
> 
> you want to know how it works, you need to either read about it, or
> 
> experiment with it.
> 
> 
> 
> Try   help("".rjust)     to see a simple description of it.  (If you're
> 
> not familiar with the interactive interpreter's help() function, you owe
> 
> it to yourself to learn it).
> 
> 
> 
> Playing with it:
> 
> 
> 
> print "abcd".rjust(8, "-")       produces    ----abcd
> 
> 
> 
> for i in range(5): print "a".rjust(i, "-")
> 
>     produces:
> 
> 
> 
> a
> 
> a
> 
> -a
> 
> --a
> 
> ---a
> 
> 
> 
> In each case, the number of characters produced is no larger than i.  No
> 
> consideration is made to other strings outside of the literal passed
> 
> into the method.
> 
> 
> 
> 
> 
> > Why it works like that? 
> 
> 
> 
> In your code, you have the rjust() method inside a loop, inside a join,
> 
> inside a print.  it makes a nice, impressive single line, but clearly
> 
> you don't completely understand what the pieces are, nor how they work
> 
> together.  Since the join is combining (concatenating) strings that are
> 
> each being produced by rjust(), it's the join() that's making this look
> 
> "relative" to you.
> 
> 
> 
> 
> 
> > 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.
> 
> 
> 
> If you want to randomly place characters on the screen, you either want
> 
> a curses-like package, or a gui.  i suspect that's not at all what you want.
> 
> 
> 
> if you want to randomly change characters in a pre-existing string,
> 
> which will then be printed to the console, then I could suggest an
> 
> approach (untested)
> 
> 
> 
> res = [" "] * length
> 
> for column in similarity:
> 
>     res[column] = "|"
> 
> res = "".join(res)
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Thanks, i've finally came to solution.

Here it is -> http://codepad.org/Q70eGkO8

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

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

[toc] | [prev] | [next] | [standalone]


#27425

Fromcrispy <ryniek90@gmail.com>
Date2012-08-19 12:25 -0700
Message-ID<45524fc1-12df-4201-8c0a-9f27f41bc402@googlegroups.com>
In reply to#27402
W dniu niedziela, 19 sierpnia 2012 19:31:30 UTC+2 użytkownik Dave Angel napisał:
> On 08/19/2012 12:25 PM, crispy wrote:
> 
> > <SNIP>
> 
> > 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.
> 
> 
> 
> rjust() does not print to the console, it just produces a string.  So if
> 
> you want to know how it works, you need to either read about it, or
> 
> experiment with it.
> 
> 
> 
> Try   help("".rjust)     to see a simple description of it.  (If you're
> 
> not familiar with the interactive interpreter's help() function, you owe
> 
> it to yourself to learn it).
> 
> 
> 
> Playing with it:
> 
> 
> 
> print "abcd".rjust(8, "-")       produces    ----abcd
> 
> 
> 
> for i in range(5): print "a".rjust(i, "-")
> 
>     produces:
> 
> 
> 
> a
> 
> a
> 
> -a
> 
> --a
> 
> ---a
> 
> 
> 
> In each case, the number of characters produced is no larger than i.  No
> 
> consideration is made to other strings outside of the literal passed
> 
> into the method.
> 
> 
> 
> 
> 
> > Why it works like that? 
> 
> 
> 
> In your code, you have the rjust() method inside a loop, inside a join,
> 
> inside a print.  it makes a nice, impressive single line, but clearly
> 
> you don't completely understand what the pieces are, nor how they work
> 
> together.  Since the join is combining (concatenating) strings that are
> 
> each being produced by rjust(), it's the join() that's making this look
> 
> "relative" to you.
> 
> 
> 
> 
> 
> > 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.
> 
> 
> 
> If you want to randomly place characters on the screen, you either want
> 
> a curses-like package, or a gui.  i suspect that's not at all what you want.
> 
> 
> 
> if you want to randomly change characters in a pre-existing string,
> 
> which will then be printed to the console, then I could suggest an
> 
> approach (untested)
> 
> 
> 
> res = [" "] * length
> 
> for column in similarity:
> 
>     res[column] = "|"
> 
> res = "".join(res)
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Thanks, i've finally came to solution.

Here it is -> http://codepad.org/Q70eGkO8

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

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

[toc] | [prev] | [next] | [standalone]


#27486

FromPeter Otten <__peter__@web.de>
Date2012-08-20 14:45 +0200
Message-ID<mailman.3551.1345466728.4697.python-list@python.org>
In reply to#27425
crispy wrote:

> Thanks, i've finally came to solution.
> 
> Here it is -> http://codepad.org/Q70eGkO8
> 
> def pairwiseScore(seqA, seqB):
> 
>     score = 0
>     bars = [str(' ') for x in seqA] # ...
>     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
> 
>     for x in similarity:
>         bars[x] = '|' # ... 
> 
>     return ''.join((seqA, '\n', ''.join(bars), '\n', seqB, '\n', 'Score: 
', str(score)))
> 

Python has a function zip() that lets you iterate over multiple sequences 
simultaneously. Instead of

for i in xrange(len(a)):
    x = a[i]
    y = b[i]
    ...

you can write

for x, y in zip(a, b):
    ...

Also, you can build the bar list immediately and avoid the similarity list.

With these changes:

def pairwise_score(a, b):
    score = 0
    was_equal = False
    bars = []
    for x, y in zip(a, b):
        equal = x == y
        if equal:
            bars.append("|")
            if was_equal:
                score += 3
            else:
                score += 1
        else:
            bars.append(" ")
            score -= 1
        was_equal = equal
    print a
    print "".join(bars)
    print b
    print "Score:", score

If you want to take this even further you can use a score matrix instead of 
if ... else:

def pairwise_score(a, b):
    score = 0
    was_equal = False
    bars = []
    matrix = [[-1, 1], [-1, 3]]
    for x, y in zip(a, b):
        equal = x == y
        score += matrix[was_equal][equal]
        bars.append(" |"[equal])
        was_equal = equal
    print a
    print "".join(bars)
    print b
    print "Score:", score

[toc] | [prev] | [next] | [standalone]


#27422

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-08-19 14:56 -0400
Message-ID<mailman.3523.1345402599.4697.python-list@python.org>
In reply to#27398
On Sun, 19 Aug 2012 09:25:03 -0700 (PDT), crispy <ryniek90@gmail.com>
declaimed the following in gmane.comp.python.general:

	<snip>

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

	str.rjust(x) will right justify "str" in a field of "x" width. This
is done by adding enough spaces to the left to make the length of the
result string (spaces + "str") equal to the specified width. The method
works on strings, not on output lines.

	Make sure your display is using fixed width fonts, not variable.

>>> strs = ["1", "to", "two", "long term", "short"]
>>> for s in strs:
... 	print s.rjust(8), s.rjust(10), s.ljust(8), s.ljust(10),
s.center(10)
... 	
       1          1 1        1              1     
      to         to to       to             to    
     two        two two      two           two    
long term  long term long term long term  long term 
   short      short short    short        short   

(Note that xjust doesn't trim a long string, which is why the line of
"long term" does not align)

>>> for s in strs:
... 	print s[:8].rjust(8), s.rjust(10), s[:8].ljust(8), s.ljust(10),
s.center(10)
... 	
       1          1 1        1              1     
      to         to to       to             to    
     two        two two      two           two    
long ter  long term long ter long term  long term 
   short      short short    short        short  

	In the above samples, I have specified an output line containing 5
"fields" of sizes 8, 10, 8, 10, 10, and using right, right, left, left,
centered justification. 


	If you need to layout a whole line, where some positions are being
set based on indexes, you need to do that as one array, not a collection
of substrings.

>>> match = [2, 7, 9, 13, 14, 24, 4]
	dummy data, note that order doesn't matter

>>> buffer = [" "] * max(match)
	build empty array sized to needed output

>>> print repr(buffer)
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
>>> for m in match:
... 	buffer[m - 1] = "|"
... 	
	replace space with pipe for each position in match (match is
presumed to count from 1, but Python indexes from 0)

>>> print repr(buffer)
[' ', '|', ' ', '|', ' ', ' ', '|', ' ', '|', ' ', ' ', ' ', '|', '|', '
', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|']
>>> output = "".join(buffer)
	join the position contents together to make the final output line

>>> print repr(output)
' | |  | |   ||         |'
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web