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


Groups > comp.lang.python > #27486

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?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!border1.nntp.ams2.giganews.com!border2.nntp.ams2.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:How': 0.09; 'python': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:characters': 0.09; 'def': 0.10; 'subject:not': 0.11; 'b):': 0.16; 'bars': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'subject:most': 0.16; 'subject:screen': 0.16; 'wrote:': 0.17; 'thanks,': 0.18; 'solution.': 0.18; 'lets': 0.22; "i've": 0.23; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'changes:': 0.29; "skip:' 10": 0.30; 'function': 0.30; 'print': 0.32; 'subject: .': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'list': 0.35; 'false': 0.35; 'subject:?': 0.35; 'list.': 0.35; 'received:org': 0.36; 'url:org': 0.36; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'build': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'further': 0.61; 'subject:, ': 0.61; 'here': 0.65; 'finally': 0.66; 'score': 0.75
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject 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?
Date Mon, 20 Aug 2012 14:45:28 +0200
Organization None
References <db49919e-9c9f-4e1f-8dfe-2765c6717dbe@googlegroups.com> <mailman.3510.1345397520.4697.python-list@python.org> <45524fc1-12df-4201-8c0a-9f27f41bc402@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849188.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3551.1345466728.4697.python-list@python.org> (permalink)
Lines 88
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1345466728 news.xs4all.nl 6846 [2001:888:2000:d::a6]:58839
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27486

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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