On Sat, 12 Nov 2005 08:12:43 +0100, Peter Otten <[EMAIL PROTECTED]> wrote:
>[EMAIL PROTECTED] wrote:
>
>> However, I found something interesting which I don't quite understand :
>>
>> list((x for x in [1,2,3] if x<2 or stop())) works
>>
>> but
>>
>> a = [ x for x in [1,2,3] if x <2 or stop() ]
Peter Otten wrote:
> [EMAIL PROTECTED] wrote:
>
> > However, I found something interesting which I don't quite understand :
> >
> > list((x for x in [1,2,3] if x<2 or stop())) works
> >
> > but
> >
> > a = [ x for x in [1,2,3] if x <2 or stop() ] doesn't.
>
> Here's how Carl Banks explained it to
[EMAIL PROTECTED] wrote:
> However, I found something interesting which I don't quite understand :
>
> list((x for x in [1,2,3] if x<2 or stop())) works
>
> but
>
> a = [ x for x in [1,2,3] if x <2 or stop() ] doesn't.
Here's how Carl Banks explained it to me when Bengt came up with this
tric
Bengt Richter wrote:
> Well, it seems you do have to put them in the scopes of different generators,
> not just for-clauses, depending on the semantics you want e.g.,
>
> >>> def stop(): raise StopIteration
> ...
> >>> list( ((x,y) for x in xrange(20) if x<5 or stop() for y in xrange(20) if
>
On Thu, 10 Nov 2005 21:46:37 -0800, [EMAIL PROTECTED] (Alex Martelli) wrote:
>[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>> > >>> list (x for x in xrange(20) if x<5 or iter([]).next())
>> > [0, 1, 2, 3, 4]
>> >
>> > Or a bit more readably:
>> > >>> def stop(): raise StopIteration
>> > ...
On 10 Nov 2005 18:20:01 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>
>Bengt Richter wrote:
>> If you want to terminate a generator expression after the first sequence of
>> elements
>> satisfying a condition, and you don't want to use takewhile, I don't know of
>> a gotcha
>> to preve
Bengt Richter wrote:
> IOW, your "when condition(x)" (IIUIC) can be spelled "if condition(x) or
> stop()"
neat trick.
--
http://mail.python.org/mailman/listinfo/python-list
oops, stand corrected. I was under the impression that an exception
would break out of the current expression and forgot that the "for"
would contain it(that StopIteration is a condition to it expects to
stop it).
thanks, this is the functionality I am looking for.
Alex Martelli wrote:
> Can you
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> > >>> list (x for x in xrange(20) if x<5 or iter([]).next())
> > [0, 1, 2, 3, 4]
> >
> > Or a bit more readably:
> > >>> def stop(): raise StopIteration
> > ...
> > >>> list (x for x in xrange(20) if x<5 or stop())
> > [0, 1, 2, 3, 4]
> >
> > IO
Bengt Richter wrote:
> If you want to terminate a generator expression after the first sequence of
> elements
> satisfying a condition, and you don't want to use takewhile, I don't know of
> a gotcha
> to prevent you from just raising StopIteration, using an expression that will
> do that, e.g.
On 10 Nov 2005 04:56:34 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>
>Peter Hansen wrote:
>> (I say "readable or somehow better" since you stated in another post "I
>> just try to use list/generator expression when possible" but you didn't
>> explain your reason for doing so. I assume
Colin J. Williams wrote:
> Are there generally accepted guidelines on what is appropriate for the
> builtin namespace?
Yes. If Guido can be convinced it should be in builtins, it should be.
Tim Delaney
--
http://mail.python.org/mailman/listinfo/python-list
Alex Martelli wrote:
> George Sakkis <[EMAIL PROTECTED]> wrote:
>...
>
FP functions like dropwhile/takewhile etc.
>>>
>>>No way -- the itertools module is and remains a PRECIOUS resource.
>>>If you want an iterator rather than a list, itertools.ifilter is quite
>>>appropriate here.
>>
>>Wh
Alex Martelli wrote:
> So use takewhile(condition, some_generator)
>
> which is LESS to type. When your predicate is a function, there's no
> need to wrap a lambda around it, just like there's no need to wrap an
> '[x for x in' or '(x for x in' around a list/iterator.
No. my predicate sometimes i
Alex Martelli wrote:
> This is the first time on this thread in which I'm glimpsing that you
> mean 'when' not as in SQL (where it has just the same meaning as the
> 'if' in Python's genexps/listcomps), but rather with the meaning that
> any Pythonista would instinctively spell 'while'. Since AFA
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
...
> takewhile(lambda x: condition(x), some_generator) is not very much
> difference than(well, still more things to type)
>
> (x for x in some_generator when condition(x))
So use takewhile(condition, some_generator)
which is LESS to type. When y
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> I use "list" in the name in "english"/general sense(say a list in
> haskell is lazily evaluated), it could be a list or it could be a
> lazily evaluated iterable.
OK, but the general point is:
[x for x in ] is best written list()
(x for x in
Peter Hansen wrote:
> (I say "readable or somehow better" since you stated in another post "I
> just try to use list/generator expression when possible" but you didn't
> explain your reason for doing so. I assume you have some reason other
> than arbitrary whim.)
The reason is simple:
I found it
[EMAIL PROTECTED] wrote:
> Alex Martelli wrote:
>
>>This becomes a valid list comprehension by writing 'if' instead of
>>'when'.
>
> valid, yes. efficient, I am not sure.
>
> [ x for x in xrange(1000) if p(x) ]
>
> means I need to go through the whole range even if p = lambda x: x < 2.
If
I use "list" in the name in "english"/general sense(say a list in
haskell is lazily evaluated), it could be a list or it could be a
lazily evaluated iterable.
The original post is really just about "when" or may be "until" syntax
that makes it a bit shorter to read and hopefuly easier to understan
[EMAIL PROTECTED] wrote:
> Leif K-Brooks wrote:
>
>>[EMAIL PROTECTED] wrote:
>>
>>>thanks. that is what I am doing now, in a more generic form :
>>>
>>>takewhile(p, (x for x in xrange(1)))
>>
>>How does a useless generator expression make it more generic?
>
> xrange is only picked as an e
Paul Rubin wrote:
> "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> > > How does a useless generator expression make it more generic?
> >
> > xrange is only picked as an example. I may be newbie on python but not
> > that dumb if all I want is a list of integer(sorted) that meets certain
> > cri
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> > How does a useless generator expression make it more generic?
>
> xrange is only picked as an example. I may be newbie on python but not
> that dumb if all I want is a list of integer(sorted) that meets certain
> criteria.
>
> takewhile(p, (x fo
Leif K-Brooks wrote:
> [EMAIL PROTECTED] wrote:
> > George Sakkis wrote:
> >
> >list(takewhile(p, xrange(1000)))
> >>
> >>[0, 1]
> >
> > thanks. that is what I am doing now, in a more generic form :
> >
> > takewhile(p, (x for x in xrange(1)))
>
> How does a useless generator expre
[EMAIL PROTECTED] wrote:
> George Sakkis wrote:
>
>list(takewhile(p, xrange(1000)))
>>
>>[0, 1]
>
> thanks. that is what I am doing now, in a more generic form :
>
> takewhile(p, (x for x in xrange(1)))
How does a useless generator expression make it more generic?
--
http://mail
"Steve Holden" <[EMAIL PROTECTED]> wrote:
> George Sakkis wrote:
> > Itertools is your friend in this case:
> >
> from itertools import takewhile
> list(takewhile(p, xrange(1000)))
> >
> > [0, 1]
>
> Maybe, but the code also implies an esoteric knowledge that the trught
> value of the
George Sakkis wrote:
> >>> list(takewhile(p, xrange(1000)))
> [0, 1]
thanks. that is what I am doing now, in a more generic form :
takewhile(p, (x for x in xrange(1)))
--
http://mail.python.org/mailman/listinfo/python-list
George Sakkis wrote:
> "[EMAIL PROTECTED]" wrote:
>
>
>>Alex Martelli wrote:
>>
>>>This becomes a valid list comprehension by writing 'if' instead of
>>>'when'.
>>
>>valid, yes. efficient, I am not sure.
>>
>>[ x for x in xrange(1000) if p(x) ]
>>
>>means I need to go through the whole range
I just try to use list/generator expression when possible and found I
need dropwhile/takewhile from time to time and see if there will be a
construct like this.
As I sort of think that the language in general encouraging this
style(like the talk about dropping map/filter/reduce).
Alex Martelli wr
"[EMAIL PROTECTED]" wrote:
> Alex Martelli wrote:
> > This becomes a valid list comprehension by writing 'if' instead of
> > 'when'.
>
> valid, yes. efficient, I am not sure.
>
> [ x for x in xrange(1000) if p(x) ]
>
> means I need to go through the whole range even if p = lambda x: x < 2
Ite
I thought I read some where there these are intended to be dropped(or
at least moved out of built-in) ?
George Sakkis wrote:
> What about the future of itertools in python 3K ? IIRC, several
> functions and methods that currently return lists are going to return
> iterators. Could this imply that
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Alex Martelli wrote:
> > This becomes a valid list comprehension by writing 'if' instead of
> > 'when'.
> valid, yes. efficient, I am not sure.
>
> [ x for x in xrange(1000) if p(x) ]
>
> means I need to go through the whole range even if p = la
George Sakkis <[EMAIL PROTECTED]> wrote:
...
> > > FP functions like dropwhile/takewhile etc.
> >
> > No way -- the itertools module is and remains a PRECIOUS resource.
> > If you want an iterator rather than a list, itertools.ifilter is quite
> > appropriate here.
>
> What about the future of
Alex Martelli wrote:
> This becomes a valid list comprehension by writing 'if' instead of
> 'when'.
valid, yes. efficient, I am not sure.
[ x for x in xrange(1000) if p(x) ]
means I need to go through the whole range even if p = lambda x: x < 2.
--
http://mail.python.org/mailman/listinfo/p
"Alex Martelli" wrote:
> [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
> > FP functions like dropwhile/takewhile etc.
>
> No way -- the itertools module is and remains a PRECIOUS resource.
> If you want an iterator rather than a list, itertools.ifilter is quite
> appropriate here.
What about th
On Nov 9, 2005, at 7:30 PM, Alex Martelli wrote:
> No way -- the itertools module is and remains a PRECIOUS resource. If
> you want an iterator rather than a list, itertools.ifilter is quite
> appropriate here.
Or if you're on 2.4 and want an iterator:
(x for x in xrange(10) if p(x))
Jay P.
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I am wondering if there is such a thing, as python is moving away from
This becomes a valid list comprehension by writing 'if' instead of
'when'.
> FP functions like dropwhile/takewhile etc.
No way -- the itertools module is and remains a
Hi,
I am wondering if there is such a thing, as python is moving away from
FP functions like dropwhile/takewhile etc.
--
http://mail.python.org/mailman/listinfo/python-list
38 matches
Mail list logo