Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[x for x in <> while <>]?

4 views
Skip to first unread message

urikaluzhny

unread,
May 15, 2008, 2:41:51 AM5/15/08
to
It seems that I rather frequently need a list or iterator of the form
[x for x in <> while <>]
And there is no one like this.
May be there is another short way to write it (not as a loop). Is
there?
Thanks

Terry Reedy

unread,
May 15, 2008, 3:06:54 AM5/15/08
to pytho...@python.org

"urikaluzhny" <ukal...@nds.com> wrote in message
news:f8229614-a000-450e...@w7g2000hsa.googlegroups.com...

| It seems that I rather frequently need a list or iterator of the form
| [x for x in <> while <>]

I can think of two ways to interpret that.

| And there is no one like this.
| May be there is another short way to write it (not as a loop). Is
| there?

Using loops to write an example of what you mean would make the above
clearer.

tjr

Bruno Desthuilliers

unread,
May 15, 2008, 3:25:47 AM5/15/08
to
urikaluzhny a écrit :

The answer is very probably in the itertools module.

Paul Rubin

unread,
May 15, 2008, 4:15:09 AM5/15/08
to

itertools.takewhile(condition, seq)

urikaluzhny

unread,
May 15, 2008, 4:17:33 AM5/15/08
to
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.edu> wrote:
> "urikaluzhny" <ukaluz...@nds.com> wrote in message

>
> news:f8229614-a000-450e...@w7g2000hsa.googlegroups.com...
> | It seems that I rather frequently need a list or iterator of the form
> | [x for x in <> while <>]
>
> I can think of two ways to interpret that.
I mean like [x for x in <A> if <B>], only that it breaks the loop when
the expression <B> is false.

Geoffrey Clements

unread,
May 15, 2008, 8:10:25 AM5/15/08
to
"urikaluzhny" <ukal...@nds.com> wrote in message
news:51e0f25d-474a-450a...@m44g2000hsc.googlegroups.com...

def gen(a):
for x in a:
if B: break
yield x

a_gen = gen(A)

# now iterate over a_gen

--
Geoff


afrobeard

unread,
May 15, 2008, 7:07:04 PM5/15/08
to
The following proposed solution is not intended to be a solution, it
goes completely against the zen of python. [Type import this into the
python command interpreter]

I brought it down to two lines:-

l = range(6)
[1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1]

itertools would still be a better approach in my opinion.

Just because I'm curious to know, can anyone bring it shorter[even if
its cryptic] than this without invoking any Python Library.

P.S. Once again I would not recommend using this as Explicit is better
than Implicit
P.P.S. It is strongly undesirable for us humans to use anything
starting with __ :)


On May 15, 5:10 pm, "Geoffrey Clements"

castironpi

unread,
May 15, 2008, 7:23:03 PM5/15/08
to
> > Geoff- Hide quoted text -
>
> - Show quoted text -

In your original, you have:

> l = range(6)
> [1 if b!=4 else l.__delslice__(0,len(l)) for b in l][:-1]

You may be hyperextending the use of '..if..else..', which is one of
my fears regarding 'with x as y'. "l.__delslice__(0,len(l))" is not
an expression.

afrobeard

unread,
May 15, 2008, 7:47:58 PM5/15/08
to
According to http://en.wikipedia.org/wiki/Expression_(programming)

"An expression in a programming language is a combination of values,
variables, operators, and functions that are interpreted (evaluated)
according to the particular rules of precedence and of association for
a particular programming language, which computes and then produces
(returns, in a stateful environment) another value."

l.__delslice__(0,len(l)) is an expression because it returns None
[which also happens to be a value] in this case.

afrobeard

unread,
May 15, 2008, 7:52:16 PM5/15/08
to
l.__delslice__(0,len(l)) is an expression as it returns None which is
a value


On May 16, 4:23 am, castironpi <castiro...@gmail.com> wrote:

castironpi

unread,
May 15, 2008, 10:26:39 PM5/15/08
to
> > an expression.- Hide quoted text -

>
> - Show quoted text -

Functional programming is really important to a former professor of
me. I like to say that None returns as a value. I think you can call
functional "evaluational".

Alexandr N Zamaraev

unread,
May 15, 2008, 11:46:33 PM5/15/08
to pytho...@python.org
urikaluzhny wrote:
>> | It seems that I rather frequently need a list or iterator of the form
>> | [x for x in <> while <>]
>> I can think of two ways to interpret that.
> I mean like [x for x in <A> if <B>], only that it breaks the loop when
> the expression <B> is false.
How do you plan to modify B during iteration?
May be
[x for x in itertools.takewhile(<B>, <A>)]
when <B> function accept element <A> and return True or False


Ruediger

unread,
May 17, 2008, 6:08:51 AM5/17/08
to
urikaluzhny wrote:

I usually have the same problem and i came up with an solution like that:

from operator import ne
def test(iterable, value, op=ne):
_n = iter(iterable).next
while True:
_x = _n()
if op(_x, value):
yield _x
else:
raise StopIteration

l = range(6)
print [x for x in test(l, 4)]

r@linux:~/tmp> python test18.py
[0, 1, 2, 3]

Paul Hankin

unread,
May 17, 2008, 6:29:16 AM5/17/08
to
On May 17, 11:08 am, Ruediger <larud...@freenet.de> wrote:
> urikaluzhny wrote:
> > It seems that I rather frequently need a list or iterator of the form
> > [x for x in <> while <>]
> > And there is no one like this.
> > May be there is another short way to write it (not as a loop). Is
> > there?
> > Thanks
>
> I usually have the same problem and i came up with an solution like that:
>
> from operator import ne
> def test(iterable, value, op=ne):
>     _n = iter(iterable).next
>     while True:
>         _x = _n()
>         if op(_x, value):
>             yield _x
>         else:
>             raise StopIteration

This is better written using takewhile...
itertools.takewhile(lambda x: x != value, iterable)

But if you really need to reinvent the wheel, perhaps this is simpler?

def test(iterable, value, op=operator.ne):
for x in iterable:
if not op(x, value):
return
yield x

--
Paul Hankin

Ruediger

unread,
May 22, 2008, 6:51:30 AM5/22/08
to
Paul Hankin wrote:


> This is better written using takewhile...
> itertools.takewhile(lambda x: x != value, iterable)
>
> But if you really need to reinvent the wheel, perhaps this is simpler?
>
> def test(iterable, value, op=operator.ne):
> for x in iterable:
> if not op(x, value):
> return
> yield x


yes you are right it is. However as i mentioned in my post i came up with an
solution 'like' that. In fact my original code was to complex to post.
While simplifying it, i've overseen the obvious solution.

For special cases where you need to do more complex tests, the best solution
is IMHO to hide it in an generator function like above.

0 new messages