Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!cs.uu.nl!news.stack.nl!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.028 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; '>>>>': 0.09; 'subject:parsing': 0.09; 'output': 0.11; 'wed,': 0.12; 'am,': 0.13; 'wrote:': 0.15; 'brackets': 0.16; 'iterator': 0.16; 'really.': 0.16; 'intermediate': 0.16; 'cc:addr:python-list': 0.16; '>>>': 0.16; 'otherwise,': 0.19; 'cc:2**0': 0.21; 'loop': 0.22; 'cc:no real name:2**0': 0.22; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'body.': 0.23; 'parse': 0.23; 'code': 0.24; 'string': 0.26; 'message-id:@mail.gmail.com': 0.28; 'import': 0.29; 'skip:i 30': 0.29; 'cc:addr:python.org': 0.30; 'logic': 0.30; 'separately.': 0.30; 'list:': 0.31; 'seem': 0.31; 'print': 0.32; 'it.': 0.33; "what's": 0.33; 'there': 0.34; '...': 0.34; 'but,': 0.34; 'rules': 0.36; 'doing': 0.37; 'but': 0.37; 'another': 0.38; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'think': 0.38; 'skip:e 20': 0.39; 'received:209': 0.40; 'where': 0.40; 'skip:r 20': 0.40; 'subjectcharset:utf-8': 0.72; '12:29': 0.84; 'algebra': 0.84; 'algorithm,': 0.84; 'balanced': 0.84; 'chemical': 0.84; 'trick?': 0.84; 'subject:little': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=hQgPxviJt26Rjl4pc5EmceAh+Uf56JP35zPJecfH34M=; b=OSUMrpaZTsxqhlvlYI6j9mBfU+ubnnxuSJj3II/7UTCXd1s6hhejjRdNGFtTDnx9nu dGGn2E0d4kEvUUpe3XiinJwzaZUCqaHvQU1E76soJXQTy1ADEcILv4J+SXlghBapMvr/ 8VabkaImu9idPBUj8/8hbCJUN/h5yBVr/Wnbw= MIME-Version: 1.0 In-Reply-To: References: <36037253-086b-4467-a1db-9492d3772e78@r5g2000prf.googlegroups.com> <8085725c-a600-4d64-8533-fd15505f94e1@p12g2000pre.googlegroups.com> <71aa9b11-5f47-4c89-bdbb-0fe4cac844e1@s33g2000prg.googlegroups.com> From: Ian Kelly Date: Wed, 20 Jul 2011 01:29:10 -0600 Subject: =?UTF-8?Q?Re=3A_a_little_parsing_challenge_=E2=98=BA?= To: jmfauth Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311146981 news.xs4all.nl 23931 [2001:888:2000:d::a6]:47153 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9947 On Wed, Jul 20, 2011 at 12:29 AM, jmfauth wrote: >> Then it is hard to code precisely. >> > > Not really. The trick is to count the different opener/closer > separately. > That is what I am doing to check balanced brackets in > chemical formulas. The rules are howerver not the same > as in math. I think the difficulty is not in the algorithm, but in adhering to the desired output when it is ambiguously described. > But, if I want to parse a string from right to left, > what's the trick? > The best I found so far: > >>>> s =3D 'abcd' >>>> for i, c in enumerate(reversed(s)): > ... =A0 =A0 print len(s) - 1 - i, c That violates DRY, since you have reversal logic in the iterator algebra and then again in the loop body. I prefer to keep all such logic in the iterator algebra, if possible. This is one possibility, if you don't mind it building an intermediate list: >>> for i, c in reversed(list(enumerate(s))): ... Otherwise, here's another non-DRY solution: >>> from itertools import izip >>> for i, c in izip(reversed(xrange(len(s))), reversed(s)): ... Unfortunately, this is one space where there just doesn't seem to be a single obvious way to do it.