Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49073 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2013-06-24 14:12 -0600 |
| Last post | 2013-06-24 23:04 -0700 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Is this PEP-able? fwhile Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-24 14:12 -0600
Re: Is this PEP-able? fwhile alex23 <wuwei23@gmail.com> - 2013-06-25 09:14 +1000
Re: Is this PEP-able? fwhile Fábio Santos <fabiosantosart@gmail.com> - 2013-06-25 00:35 +0100
Re: Is this PEP-able? fwhile alex23 <wuwei23@gmail.com> - 2013-06-25 10:00 +1000
Re: Is this PEP-able? fwhile Fábio Santos <fabiosantosart@gmail.com> - 2013-06-25 01:19 +0100
Re: Is this PEP-able? fwhile wu wei <wuwei23@gmail.com> - 2013-06-25 10:41 +1000
Re: Is this PEP-able? fwhile Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-24 22:50 -0600
Re: Is this PEP-able? fwhile rusi <rustompmody@gmail.com> - 2013-06-24 23:04 -0700
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-06-24 14:12 -0600 |
| Subject | Re: Is this PEP-able? fwhile |
| Message-ID | <mailman.3760.1372104769.3114.python-list@python.org> |
On Mon, Jun 24, 2013 at 1:52 PM, <jimjhb@aol.com> wrote:
> Syntax:
>
> fwhile X in ListY and conditionZ:
>
> The following would actually exactly as: for X in ListY:
>
> fwhile X in ListY and True:
>
> fwhile would act much like 'for', but would stop if the condition after the
> 'and' is no longer True.
>
> The motivation is to be able to make use of all the great aspects of the
> python 'for' (no indexing or explict
> end condition check, etc.) and at the same time avoiding a 'break' from the
> 'for'.
I would advocate using the break myself. Another alternative is this:
for X in itertools.takewhile(lambda X: conditionZ, ListY):
...
[toc] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-06-25 09:14 +1000 |
| Message-ID | <kqajj8$54h$1@dont-email.me> |
| In reply to | #49073 |
On 25/06/2013 6:12 AM, Ian Kelly wrote:
> On Mon, Jun 24, 2013 at 1:52 PM, <jimjhb@aol.com> wrote:
>> Syntax:
>> fwhile X in ListY and conditionZ:
>>
>> fwhile would act much like 'for', but would stop if the condition after the
>> 'and' is no longer True.
>
> I would advocate using the break myself. Another alternative is this:
>
> for X in itertools.takewhile(lambda X: conditionZ, ListY):
> ...
I'd probably just go with a generator expression to feed the for loop:
for X in (i for i in ListY if conditionZ):
....
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-25 00:35 +0100 |
| Message-ID | <mailman.3788.1372116912.3114.python-list@python.org> |
| In reply to | #49106 |
[Multipart message — attachments visible in raw view] — view raw
On 25 Jun 2013 00:31, "alex23" <wuwei23@gmail.com> wrote: > > On 25/06/2013 6:12 AM, Ian Kelly wrote: >> >> On Mon, Jun 24, 2013 at 1:52 PM, <jimjhb@aol.com> wrote: >>> >>> Syntax: >>> fwhile X in ListY and conditionZ: >>> >>> fwhile would act much like 'for', but would stop if the condition after the >>> 'and' is no longer True. >> >> >> I would advocate using the break myself. Another alternative is this: >> >> for X in itertools.takewhile(lambda X: conditionZ, ListY): >> ... > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2013-06-25 10:00 +1000 |
| Message-ID | <kqam8g$f7n$1@dont-email.me> |
| In reply to | #49109 |
On 25/06/2013 9:35 AM, Fábio 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.
[toc] | [prev] | [next] | [standalone]
| From | Fábio Santos <fabiosantosart@gmail.com> |
|---|---|
| Date | 2013-06-25 01:19 +0100 |
| Message-ID | <mailman.3790.1372120016.3114.python-list@python.org> |
| In reply to | #49112 |
[Multipart message — attachments visible in raw view] — view raw
On 25 Jun 2013 01:08, "alex23" <wuwei23@gmail.com> wrote:
>
> On 25/06/2013 9:35 AM, Fábio 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.
[toc] | [prev] | [next] | [standalone]
| From | wu wei <wuwei23@gmail.com> |
|---|---|
| Date | 2013-06-25 10:41 +1000 |
| Message-ID | <mailman.3791.1372120877.3114.python-list@python.org> |
| In reply to | #49112 |
[Multipart message — attachments visible in raw view] — view raw
On Tue, Jun 25, 2013 at 10:19 AM, Fábio Santos <fabiosantosart@gmail.com>
wrote:
> 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.
>
Ah yes, of course, my bad.
It's still possible by raising a StopIteration within the condition
function:
def is_part_of_header(x):
if header_condition:
return True
else:
raise StopIteration
But yes, by this point any clarity of the generator expression approach
comes with the cost of more explicit setup of the breaking condition.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-06-24 22:50 -0600 |
| Message-ID | <mailman.3798.1372135909.3114.python-list@python.org> |
| In reply to | #49112 |
On Mon, Jun 24, 2013 at 6:41 PM, wu wei <wuwei23@gmail.com> wrote: > It's still possible by raising a StopIteration within the condition > function: > > def is_part_of_header(x): > if header_condition: > return True > else: > raise StopIteration Which is basically just taking the break and moving it to somewhere else in the code, and meanwhile rendering the is_part_of_header function non-reusable.
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-06-24 23:04 -0700 |
| Message-ID | <3f0b9da0-672a-4eb6-9e92-5eb215fc958f@googlegroups.com> |
| In reply to | #49106 |
On Tuesday, June 25, 2013 4:44:44 AM UTC+5:30, alex23 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): > > .... Nice idiom -- thanks Yes it does not correspond to a takewhile (or break in the control structure world). It does correspond to a filter
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web