Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed1.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'processing.': 0.07; 'stops': 0.07; 'subject:PEP': 0.07; 'lines.': 0.09; 'cc:addr :python-list': 0.11; '>>': 0.16; 'expression,': 0.16; 'expression.': 0.16; 'iterables': 0.16; 'iterated': 0.16; 'once.': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'example': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'load': 0.23; 'header': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'checking': 0.33; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'too': 0.37; 'list': 0.37; 'skip:& 10': 0.38; 'feed': 0.38; 'sure': 0.39; 'even': 0.60; 'expression': 0.60; 'subject:? ': 0.60; 'took': 0.61; 'to:addr:gmail.com': 0.65; 'subject:this': 0.83; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Mo2ygFbWMSbcFl9T7zJYXztsSO4hueeIcBEipmJ8R5E=; b=RXp0IYVswl8R002fXBTj9Bi8m6Iy8IVPAx6OKfXJttZIGwviiaShWOspJTuxiEVhJM +yWQJ/02lbRzzt2nrT6G6ouZEPnqp3tkavUuvCH/NjoE8FegoFkk491eVTBkjFEL+Ofd hReO5TXEy55+2EdEjIvp7H41QxRQ3bBpwrS4F3YnNmH6P5oei3a4wZcaomwD7WSE3t/O H6XSGC+9KL4ickenbo2gMrpaQWGIzRQ+QI4w/0WO87nJVPD7+00A6+aQfcn993HBLSUa 3nnblrKK1mJtWzD4U3yKj4WOeWb1zK48ajQcsT6EcSpi8JlJNep1xYuwZXHWL9xZA0E0 nlpg== MIME-Version: 1.0 X-Received: by 10.49.16.233 with SMTP id j9mr29176189qed.12.1372119569230; Mon, 24 Jun 2013 17:19:29 -0700 (PDT) In-Reply-To: References: <8D03F2B8CF0E7BE-1864-1796B@webmail-m103.sysops.aol.com> Date: Tue, 25 Jun 2013 01:19:29 +0100 Subject: Re: Is this PEP-able? fwhile From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: alex23 Content-Type: multipart/alternative; boundary=047d7bea38e283753604dfef7bbe Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1372120016 news.xs4all.nl 15863 [2001:888:2000:d::a6]:39477 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:49114 --047d7bea38e283753604dfef7bbe Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On 25 Jun 2013 01:08, "alex23" wrote: > > On 25/06/2013 9:35 AM, F=E1bio Santos wrote: >> >> > I'd probably just go with a generator expression to feed the for loop= : >> > >> > for X in (i for i in ListY if conditionZ): >> > .... >> >> That is nice but it's not lazy. If the condition or the iterables took >> too long to compute, it would be troublesome. > > > I'm not sure I follow. It's a generator expression, not a list comprehension, so the condition will be evaluated per item iterated over in the generator, not across all valid items in ListY at once. > for X in (i for i in open('largefile') if is_part_of_header(i)): The above code would be wasting time on IO and processing. It would load another line and calculate the condition for every line of the large file and I just wanted to loop over the few header lines. itertools.takewhile and fwhile/for..while actually stops the loop when the condition is not meant, while your example keeps checking the condition until the end of file, even though it is a generator expression. --047d7bea38e283753604dfef7bbe Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On 25 Jun 2013 01:08, "alex23" <wuwei23@gmail.com> wrote:
>
> On 25/06/2013 9:35 AM, F=E1bio Santos wrote:
>>
>> =A0> I'd probably just go with a generator expression to fe= ed the for loop:
>> =A0>
>> =A0> =A0 =A0 for X in (i for i in ListY if conditionZ):
>> =A0> =A0 =A0 =A0 =A0 ....
>>
>> That is nice but it's not lazy. If the condition or the iterab= les took
>> too long to compute, it would be troublesome.
>
>
> I'm not sure I follow. It's a generator expression, not a list= comprehension, so the condition will be evaluated per item iterated over i= n the generator, not across all valid items in ListY at once.
>

for X in (i for i in open('largefile') if is_part_of= _header(i)):

The above code would be wasting time on IO and processing. I= t would load another line and calculate the condition for every line of the= large file and I just wanted to loop over the few header lines.

itertools.takewhile and fwhile/for..while actually stops the= loop when the condition is not meant, while your example keeps checking th= e condition until the end of file, even though it is a generator expression= .

--047d7bea38e283753604dfef7bbe--