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: 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: <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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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