473,408 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

[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
Jun 27 '08 #1
14 1247

"urikaluzhny" <uk*******@nds.comwrote in message
news:f8**********************************@w7g2000h sa.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

Jun 27 '08 #2
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.

Jun 27 '08 #3
urikaluzhny <uk*******@nds.comwrites:
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)
Jun 27 '08 #4
On May 15, 10:06*am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message

news:f8**********************************@w7g2000h sa.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 <Aif <B>], only that it breaks the loop when
the expression <Bis false.
Jun 27 '08 #5
"urikaluzhny" <uk*******@nds.comwrote in message
news:51**********************************@m44g2000 hsc.googlegroups.com...
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message

news:f8**********************************@w7g2000h sa.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 <Aif <B>], only that it breaks the loop when
the expression <Bis false.
def gen(a):
for x in a:
if B: break
yield x

a_gen = gen(A)

# now iterate over a_gen

--
Geoff
Jun 27 '08 #6
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"
<geoffrey.clement...@SPAMbaesystems.comwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message

news:51**********************************@m44g2000 hsc.googlegroups.com...
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
news:f8**********************************@w7g2000h sa.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 <Aif <B>], only that it breaks the loop when
the expression <Bis false.

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

a_gen = gen(A)

# now iterate over a_gen

--
Geoff
Jun 27 '08 #7
On May 15, 6:07*pm, afrobeard <afrobe...@gmail.comwrote:
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"

<geoffrey.clement...@SPAMbaesystems.comwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
news:51**********************************@m44g2000 hsc.googlegroups.com...
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
>news:f8**********************************@w7g2000 hsa.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 <Aif <B>], only that it breaks the loop when
>the expression <Bis false.
def gen(a):
* * for x in a:
* * * * if B: break
* * * * yield x
a_gen = gen(A)
# now iterate over a_gen
--
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.
Jun 27 '08 #8
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.comwrote:
On May 15, 6:07*pm, afrobeard <afrobe...@gmail.comwrote:
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"
<geoffrey.clement...@SPAMbaesystems.comwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
>news:51**********************************@m44g200 0hsc.googlegroups.com....
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
news:f8**********************************@w7g2000h sa.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 <Aif <B>], only that it breaks the loop when
the expression <Bis false.
def gen(a):
* * for x in a:
* * * * if B: break
* * * * yield x
a_gen = gen(A)
# now iterate over a_gen
--
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.
Jun 27 '08 #9
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.comwrote:
On May 15, 6:07*pm, afrobeard <afrobe...@gmail.comwrote:
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"
<geoffrey.clement...@SPAMbaesystems.comwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
>news:51**********************************@m44g200 0hsc.googlegroups.com....
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
news:f8**********************************@w7g2000h sa.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 <Aif <B>], only that it breaks the loop when
the expression <Bis false.
def gen(a):
* * for x in a:
* * * * if B: break
* * * * yield x
a_gen = gen(A)
# now iterate over a_gen
--
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.
Jun 27 '08 #10
On May 15, 6:52*pm, afrobeard <afrobe...@gmail.comwrote:
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.comwrote:
On May 15, 6:07*pm, afrobeard <afrobe...@gmail.comwrote:
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"
<geoffrey.clement...@SPAMbaesystems.comwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
news:51**********************************@m44g2000 hsc.googlegroups.com...
On May 15, 10:06 am, "Terry Reedy" <tjre...@udel.eduwrote:
"urikaluzhny" <ukaluz...@nds.comwrote in message
>news:f8**********************************@w7g2000 hsa.googlegroups.com...
| It seems that I rather frequently need a list or iterator of theform
| [x for x in <while <>]
I can think of two ways to interpret that.
>I mean like [x for x in <Aif <B>], only that it breaks the loopwhen
>the expression <Bis false.
def gen(a):
* * for x in a:
* * * * if B: break
* * * * yield x
a_gen = gen(A)
# now iterate over a_gen
--
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.- 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".
Jun 27 '08 #11
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 <Aif <B>], only that it breaks the loop when
the expression <Bis false.
How do you plan to modify B during iteration?
May be
[x for x in itertools.takewhile(<B>, <A>)]
when <Bfunction accept element <Aand return True or False
Jun 27 '08 #12
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:~/tmppython test18.py
[0, 1, 2, 3]

Jun 27 '08 #13
On May 17, 11:08*am, Ruediger <larud...@freenet.dewrote:
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
Jun 27 '08 #14
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.

Jun 27 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: Donald Firesmith | last post by:
I am having trouble having Google Adsense code stored in XSL converted properly into HTML. The <> unfortunately become &lt; and &gt; and then no longer work. XSL code is: <script...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: JezB | last post by:
I'm binding a DataGrid web-control to data fetched from a database. However some of my data fields contain text that is within <...> characters - I notice that everything between the <> is...
3
by: | last post by:
I have been researching articles on google on how to create a simple RSS feed that sucks <title><blurb><link><date> out of a sql server 2000 database via an aspx page. I know it has to be pushed...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
1
by: mike | last post by:
I've got some code like this: gametype_id = Request.Form("gametype_id") response.write "<br>gametype_id from form>" & gametype_id & "<" response.write "<br>gametype_id from database>" &...
3
by: ajay2552 | last post by:
Hi, I have a query. All html tags start with < and end with >. Suppose i want to display either '<' or '>' or say some text like '<Company>' in html how do i do it? One method is to use &lt,...
14
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the...
3
by: Josh Valino | last post by:
Hi, I have a client that has our product and in one of the aspx files, there is code like this: <%= SomePublicProperty %> where the public property returns a string. In the test...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.