Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  15 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
urikaluzhny  
View profile  
 More options May 15 2008, 2:41 am
Newsgroups: comp.lang.python
From: urikaluzhny <ukaluz...@nds.com>
Date: Wed, 14 May 2008 23:41:51 -0700 (PDT)
Local: Thurs, May 15 2008 2:41 am
Subject: [x for x in <> while <>]?
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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options May 15 2008, 3:06 am
Newsgroups: comp.lang.python
From: "Terry Reedy" <tjre...@udel.edu>
Date: Thu, 15 May 2008 03:06:54 -0400
Local: Thurs, May 15 2008 3:06 am
Subject: Re: [x for x in <> while <>]?

"urikaluzhny" <ukaluz...@nds.com> wrote in message

news:f8229614-a000-450e-85eb-825e6c1386cf@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruno Desthuilliers  
View profile  
 More options May 15 2008, 3:25 am
Newsgroups: comp.lang.python
From: Bruno Desthuilliers <bruno.42.desthuilli...@websiteburo.invalid>
Date: Thu, 15 May 2008 09:25:47 +0200
Local: Thurs, May 15 2008 3:25 am
Subject: Re: [x for x in <> while <>]?
urikaluzhny a écrit :

> 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?

The answer is very probably in the itertools module.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Rubin  
View profile  
 More options May 15 2008, 4:15 am
Newsgroups: comp.lang.python
From: Paul Rubin <http://phr...@NOSPAM.invalid>
Date: 15 May 2008 01:15:09 -0700
Local: Thurs, May 15 2008 4:15 am
Subject: Re: [x for x in <> while <>]?

urikaluzhny <ukaluz...@nds.com> writes:
> 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?

itertools.takewhile(condition, seq)

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
urikaluzhny  
View profile  
 More options May 15 2008, 4:17 am
Newsgroups: comp.lang.python
From: urikaluzhny <ukaluz...@nds.com>
Date: Thu, 15 May 2008 01:17:33 -0700 (PDT)
Local: Thurs, May 15 2008 4:17 am
Subject: Re: ?
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.edu> wrote:
> "urikaluzhny" <ukaluz...@nds.com> wrote in message

> news:f8229614-a000-450e-85eb-825e6c1386cf@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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Geoffrey Clements  
View profile  
 More options May 15 2008, 8:10 am
Newsgroups: comp.lang.python
From: "Geoffrey Clements" <geoffrey.clement...@SPAMbaesystems.com>
Date: Thu, 15 May 2008 13:10:25 +0100
Local: Thurs, May 15 2008 8:10 am
Subject: Re: ?
"urikaluzhny" <ukaluz...@nds.com> wrote in message

news:51e0f25d-474a-450a-ad00-92f70c893c6c@m44g2000hsc.googlegroups.com...
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.edu> wrote:

> "urikaluzhny" <ukaluz...@nds.com> wrote in message

> news:f8229614-a000-450e-85eb-825e6c1386cf@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.

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

a_gen = gen(A)

# now iterate over a_gen

--
Geoff


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
afrobeard  
View profile  
 More options May 15 2008, 7:07 pm
Newsgroups: comp.lang.python
From: afrobeard <afrobe...@gmail.com>
Date: Thu, 15 May 2008 16:07:04 -0700 (PDT)
Local: Thurs, May 15 2008 7:07 pm
Subject: Re: ?
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"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
castironpi  
View profile  
 More options May 15 2008, 7:23 pm
Newsgroups: comp.lang.python
From: castironpi <castiro...@gmail.com>
Date: Thu, 15 May 2008 16:23:03 -0700 (PDT)
Local: Thurs, May 15 2008 7:23 pm
Subject: Re: ?
On May 15, 6:07 pm, afrobeard <afrobe...@gmail.com> wrote:

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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
afrobeard  
View profile  
 More options May 15 2008, 7:47 pm
Newsgroups: comp.lang.python
From: afrobeard <afrobe...@gmail.com>
Date: Thu, 15 May 2008 16:47:58 -0700 (PDT)
Local: Thurs, May 15 2008 7:47 pm
Subject: Re: ?
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.

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
afrobeard  
View profile  
 More options May 15 2008, 7:52 pm
Newsgroups: comp.lang.python
From: afrobeard <afrobe...@gmail.com>
Date: Thu, 15 May 2008 16:52:16 -0700 (PDT)
Local: Thurs, May 15 2008 7:52 pm
Subject: Re: ?
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
castironpi  
View profile  
 More options May 15 2008, 10:26 pm
Newsgroups: comp.lang.python
From: castironpi <castiro...@gmail.com>
Date: Thu, 15 May 2008 19:26:39 -0700 (PDT)
Local: Thurs, May 15 2008 10:26 pm
Subject: Re: ?
On May 15, 6:52 pm, afrobeard <afrobe...@gmail.com> wrote:

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".

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alexandr N Zamaraev  
View profile  
 More options May 15 2008, 11:46 pm
Newsgroups: comp.lang.python
From: Alexandr N Zamaraev <to...@promsoft.ru>
Date: Fri, 16 May 2008 10:46:33 +0700
Local: Thurs, May 15 2008 11:46 pm
Subject: Re: ?
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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ruediger  
View profile  
 More options May 17 2008, 6:08 am
Newsgroups: comp.lang.python
From: Ruediger <larud...@freenet.de>
Date: Sat, 17 May 2008 12:08:51 +0200
Local: Sat, May 17 2008 6:08 am
Subject: Re: [x for x in <> while <>]?

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

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

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul Hankin  
View profile  
 More options May 17 2008, 6:29 am
Newsgroups: comp.lang.python
From: Paul Hankin <paul.han...@gmail.com>
Date: Sat, 17 May 2008 03:29:16 -0700 (PDT)
Local: Sat, May 17 2008 6:29 am
Subject: Re: ?
On May 17, 11:08 am, Ruediger <larud...@freenet.de> 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

--
Paul Hankin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ruediger  
View profile  
 More options May 22 2008, 6:51 am
Newsgroups: comp.lang.python
From: Ruediger <larud...@freenet.de>
Date: Thu, 22 May 2008 12:51:30 +0200
Local: Thurs, May 22 2008 6:51 am
Subject: Re: ?

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »