Re: [ x for x in xrange(10) when p(x) ]

2005-11-12 Thread Bengt Richter
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() ]

Re: [ x for x in xrange(10) when p(x) ]

2005-11-12 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-11 Thread Peter Otten
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-11 Thread [EMAIL PROTECTED]
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 >

Re: [ x for x in xrange(10) when p(x) ]

2005-11-11 Thread Bengt Richter
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 >> > ...

Re: [ x for x in xrange(10) when p(x) ]

2005-11-11 Thread Bengt Richter
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Alex Martelli
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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.

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Bengt Richter
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

RE: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Delaney, Timothy (Tim)
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Colin J. Williams
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Alex Martelli
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Alex Martelli
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Peter Hansen
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Leif K-Brooks
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Paul Rubin
"[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-10 Thread Leif K-Brooks
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread George Sakkis
"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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread Steve Holden
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread George Sakkis
"[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread Alex Martelli
[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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread Alex Martelli
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread [EMAIL PROTECTED]
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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread George Sakkis
"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

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread Jay Parlar
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.

Re: [ x for x in xrange(10) when p(x) ]

2005-11-09 Thread Alex Martelli
[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

[ x for x in xrange(10) when p(x) ]

2005-11-09 Thread [EMAIL PROTECTED]
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