Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44196
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.015 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'lines,': 0.07; 'nested': 0.07; 'iterate': 0.09; 'worked.': 0.09; '24,': 0.16; 'caveat': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'ignoring': 0.16; 'iterable': 0.16; 'pairs': 0.16; 'pairs,': 0.16; 'roy': 0.16; 'wrote:': 0.18; '(not': 0.18; 'wed,': 0.18; 'instance,': 0.24; 'file.': 0.24; 'mention': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'catching': 0.31; 'terminate': 0.31; 'there,': 0.34; "i'd": 0.34; 'problem': 0.35; 'definition': 0.35; 'received:google.com': 0.35; 'returning': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'mentioned': 0.61; 'today,': 0.61; 'simply': 0.61; 'simple': 0.61; '(that': 0.65; 'smith': 0.68; 'legal': 0.71; 'guaranteed': 0.75; "else's": 0.84; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=AycvvdxOx/4J++OL4WO/4j7SOmlI71YF71F3KLsXhZw=; b=F0ZIOQ2s7WlhT1jpXlon/p2gBSTMiIeke1QUnxATvVopoWKMWQRz+cj4DwdsvWaMB9 lx7nk6w/lu4og0EcwY1Nz75DUZjocHadsyAPPHyaKs4cSmT+Ur9R0n5yV+95yMTQ5Uji is+vYOTfWz/AgJW9TSFoRtrqFhdbu83LnVG2AZaR7Yb3fmGMtusMHIbIleJgUb2EvJCt DNGkda4tVwpzF6u/tFPwUWPzMPTRUv97a8ybAr5Xt2IxC3SaCo2K+U7oZEjgfWsBg96d d6/PA4vtD2kWwOeQ89kqd9CJ0cw1c0C6Bi7Tp2yvaZpPLrAZ+wteOij6ztH24+LSS8xR Fv5Q== |
| MIME-Version | 1.0 |
| X-Received | by 10.58.56.161 with SMTP id b1mr23188635veq.42.1366734085903; Tue, 23 Apr 2013 09:21:25 -0700 (PDT) |
| In-Reply-To | <kl6a1f$k2l$1@panix2.panix.com> |
| References | <kl6a1f$k2l$1@panix2.panix.com> |
| Date | Wed, 24 Apr 2013 02:21:25 +1000 |
| Subject | Re: Nested iteration? |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.983.1366734096.3114.python-list@python.org> (permalink) |
| Lines | 33 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366734096 news.xs4all.nl 2294 [2001:888:2000:d::a6]:38345 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44196 |
Show key headers only | View raw
On Wed, Apr 24, 2013 at 1:40 AM, Roy Smith <roy@panix.com> wrote: > In reviewing somebody else's code today, I found the following > construct (eliding some details): > > f = open(filename) > for line in f: > if re.search(pattern1, line): > outer_line = f.next() > for inner_line in f: > if re.search(pattern2, inner_line): > inner_line = f.next() > > Somewhat to my surprise, the code worked. I didn't know it was legal > to do nested iterations over the same iterable (not to mention mixing > calls to next() with for-loops). Is this guaranteed to work in all > situations? The definition of the for loop is sufficiently simple that this is safe, with the caveat already mentioned (that __iter__ is just returning self). And calling next() inside the loop will simply terminate the loop if there's nothing there, so I'd not have a problem with code like that - for instance, if I wanted to iterate over pairs of lines, I'd happily do this: for line1 in f: line2=next(f) print(line2) print(line1) That'll happily swap pairs, ignoring any stray line at the end of the file. Why bother catching StopIteration just to break? ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Nested iteration? roy@panix.com (Roy Smith) - 2013-04-23 11:40 -0400
Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 17:05 +0100
Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:05 -0600
Re: Nested iteration? Peter Otten <__peter__@web.de> - 2013-04-23 18:15 +0200
Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:21 +1000
Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:35 +0000
Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:30 -0600
Re: Nested iteration? Ian Kelly <ian.g.kelly@gmail.com> - 2013-04-23 10:39 -0600
Re: Nested iteration? Chris Angelico <rosuav@gmail.com> - 2013-04-24 02:42 +1000
Re: Nested iteration? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-23 16:53 +0000
Re: Nested iteration? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-23 16:49 -0400
Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:14 +0100
Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 22:29 +0100
Re: Nested iteration? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-23 22:41 +0100
Re: Nested iteration? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-23 23:42 +0100
csiph-web