Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44194
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.026 |
| X-Spam-Evidence | '*H*': 0.95; '*S*': 0.00; 'explicitly': 0.05; 'nested': 0.07; 'latter': 0.09; 'list).': 0.09; 'worked.': 0.09; '(x,': 0.16; '23,': 0.16; 'iterable': 0.16; 'iteration': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'looping': 0.16; 'object)': 0.16; 'roy': 0.16; 'wrote:': 0.18; '(not': 0.18; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'getting': 0.31; 'object.': 0.31; 'file': 0.32; 'received:209.85': 0.35; 'case,': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'subject:?': 0.36; 'two': 0.37; 'received:209': 0.37; 'somebody': 0.38; 'to:addr:python-list': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'break': 0.61; 'today,': 0.61; 'simply': 0.61; 'different': 0.65; 'smith': 0.68; 'results': 0.69; 'legal': 0.71; 'guaranteed': 0.75; "else's": 0.84; 'replicate': 0.84; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=sQKkPJ0SOoS32PRvCSwxsNmdo7iSmPFurRWsUvdfWGM=; b=c3kToLGKZTW68bZ8byJ8nWycSdBOhY0QnbFISZ42gZGH1zxgRKr4R/IKJoX3PFZnJp WM9+0vjzGgaAdzuZTPsuB4w8/anRGZH8O7gNYeHH2WVw7F2bNJmNieMYZk+v2NAatYWb 25e7d6u+3lFTDq9iTfzRb9QoF/2Sk4kVFwO3Je/41lCGSj/WQWLr79tAZxBuLK8YBRKn pSUQVhHAY8lx4oSFsqJNU2e+M/fjB5pJvQofaJERoDi9V7AxCZxPkttJTFlNE8R8MgIm HbV1iFDaiyYACHtyQO5AIzTmqSqxmOMl0h2hA93uCkpYmc0HGSlsWOd0LdxFFyA4vJjj w2mA== |
| X-Received | by 10.68.176.197 with SMTP id ck5mr41331658pbc.165.1366733166425; Tue, 23 Apr 2013 09:06:06 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <kl6a1f$k2l$1@panix2.panix.com> |
| References | <kl6a1f$k2l$1@panix2.panix.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Tue, 23 Apr 2013 10:05:26 -0600 |
| Subject | Re: Nested iteration? |
| To | Python <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.981.1366733175.3114.python-list@python.org> (permalink) |
| Lines | 32 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366733175 news.xs4all.nl 2244 [2001:888:2000:d::a6]:55630 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:44194 |
Show key headers only | View raw
On Tue, Apr 23, 2013 at 9: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?
Yes, although the results will be different depending on whether the
iterable stores its iteration state on itself (like a file object) or
in the iterator (like a list). In the latter case, you would simply
have two independent simultaneous iterations of the same object. You
can replicate the same effect in the latter case though by getting an
iterator from the object and explicitly looping over the same
iterator, like so:
i = iter(range(10))
for x in i:
if x % 4 == 1:
for y in i:
if y % 4 == 3:
print("%d + %d = %d" % (x, y, x+y))
break
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