Hello,
I have seen a lot of way to use the for statement, but I still don't know
how to do:
for i=morethanzero to n
or
for i= 5 to len(var)
of course I kno wthat I can use a while loop to achieve that but if this is
possible whith a for one, my preference goes to the for.
regards,
Dominique. 7 1599
houbahop wrote: for i=morethanzero to n
for i in range(morethanzero, n):
...
for i= 5 to len(var)
for i in range(5, len(var)):
...
although range(len(var)) is usually not what you want, because you can
just do:
for item in itertools.islice(var, 5, None):
...
or if you really do need the index too:
for i, item in itertools.islice(enumerate(var), 5, None):
...
Steve
> for i in range(morethanzero, n): ...
for i= 5 to len(var)
for i in range(5, len(var)): ...
Better use xrange - it doesn't create an actual list, but instead an
iterator enumerating the elements. That way more memory and cpu efficient.
--
Regards,
Diez B. Roggisch
Diez B. Roggisch wrote: for i in range(5, len(var)): ... Better use xrange - it doesn't create an actual list, but instead an iterator enumerating the elements. That way more memory and cpu efficient.
I could've sworn I read somewhere in the past that xrange was supposed
to be slower than range in some situation, but at least for some simple
cases in Python 2.4, it seems to be about as fast or faster:
python -m timeit "for i in range(100): a = i*2"
10000 loops, best of 3: 21.8 usec per loop
python -m timeit "for i in xrange(100): a = i*2"
10000 loops, best of 3: 19.4 usec per loop
python -m timeit "for i in range(10000): a = i*2"
100 loops, best of 3: 2.18 msec per loop
python -m timeit "for i in xrange(10000): a = i*2"
100 loops, best of 3: 2.29 msec per loop
python -m timeit "for i in range(100000): a = i*2"
10 loops, best of 3: 25.5 msec per loop
python -m timeit "for i in xrange(100000): a = i*2"
10 loops, best of 3: 20.7 msec per loop
I hardly ever use (x)range for anything (because you can almost always
use a for loop to loop over items instead), but I'll file this info away
mentally for the next time I do. Thanks!
Steve
houbahop wrote: Hello, I have seen a lot of way to use the for statement, but I still don't
know how to do:
for i=morethanzero to n or for i= 5 to len(var)
of course I kno wthat I can use a while loop to achieve that but if
this is possible whith a for one, my preference goes to the for.
regards, Dominique.
I think the key point that hasn't been made here is what a for
statement is really doing in python...
From the online documentation:
"The for statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object"
Neither of the statements: for i=morethanzero to n for i= 5 to len(var)
create a sequence or iterable object, and that is why they don't work.
That is why previous posts in this thread have suggested using range(),
xrange(), etc. Because they create a sequence or iterable object.
When first using python, I recall that this distinction was not clear
to me, as I was used to a more traditional for statement (as in
C/C++):
for ( initialise ; test ; update )
Essentially, python's for statement is more like a foreach statement in
many other languages (Perl, C#, etc). These statements essentially
reduce the traditional form above to what many consider a more readable
form:
foreach item in collection:
In order to transform the tradition for statement into this more
readable form, each language requires that their collections being
iterated over satisfy a precondition defined by the language (in python
this precondition is that the collection is iterable).
While this restriction may seem a little strange to people who are used
to the more traditional for statement form, the result of this approach
is often a more readable and clear statement.
Regards,
Michael Loritsch
thank you,
Whith python, It seems that all that I have learn in other languages is not
directly usable :) but looks like python is more efficient.
dominique.
<lo******@gmail.com> a écrit dans le message de news: 11**********************@c13g2000cwb.googlegroups. com... houbahop wrote: Hello, I have seen a lot of way to use the for statement, but I still don't know how to do:
for i=morethanzero to n or for i= 5 to len(var)
of course I kno wthat I can use a while loop to achieve that but if this is possible whith a for one, my preference goes to the for.
regards, Dominique.
I think the key point that hasn't been made here is what a for statement is really doing in python...
From the online documentation:
"The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object"
Neither of the statements: for i=morethanzero to n for i= 5 to len(var) create a sequence or iterable object, and that is why they don't work.
That is why previous posts in this thread have suggested using range(), xrange(), etc. Because they create a sequence or iterable object. When first using python, I recall that this distinction was not clear to me, as I was used to a more traditional for statement (as in C/C++):
for ( initialise ; test ; update )
Essentially, python's for statement is more like a foreach statement in many other languages (Perl, C#, etc). These statements essentially reduce the traditional form above to what many consider a more readable form:
foreach item in collection:
In order to transform the tradition for statement into this more readable form, each language requires that their collections being iterated over satisfy a precondition defined by the language (in python this precondition is that the collection is iterable).
While this restriction may seem a little strange to people who are used to the more traditional for statement form, the result of this approach is often a more readable and clear statement. Regards,
Michael Loritsch
> I hardly ever use (x)range for anything (because you can almost always use a for loop to loop over items instead), but I'll file this info away mentally for the next time I do. Thanks!
Thats true for me too - and since enumerate was introduced, the number of
applications of xrange have decreased further.
--
Regards,
Diez B. Roggisch
If you are used to the C kind of for loops, avoid mistakes like this one: for i in range(1,6):
.... print i
.... i+=2
....
1
2
3
4
5
Obvious if you know you are iterating over a sequence. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Charles Alexander |
last post by:
Hello
I am new to php & MySQL - I am trying to retrieve some records from a MySQL
table and redisplay them. The data in list form looks like this:
Sample_ID Marker_ID Variation
...
|
by: Anand Pillai |
last post by:
This is for folks who are familiar with asynchronous event handling in
Python using the asyncore module.
If you have ever used the asyncore module, you will realize that it's
event loop does not...
|
by: Gremlin |
last post by:
If you are not familiar with the halting problem, I will not go into
it in detail but it states that it is impossible to write a program
that can tell if a loop is infinite or not. This is a...
|
by: Martin Schou |
last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C,
which explains why I'm doing an exercise like this.
In the following tripple nested loop:
int digit1 = 1;
int digit2 = 0;...
|
by: Toby Newman |
last post by:
At the page:
http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000
or
http://tinyurl.com/4ptzs
the author warns:
"The for loop is frequently used, usually...
|
by: Alex |
last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland
Platform - Win32 (XP)
Quite by accident I stumbled...
|
by: Ben R. |
last post by:
In an article I was reading
(http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/),
I read the following:
"The ending condition of a VB.NET for loop is evaluated only once,...
|
by: cj |
last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of
the loop using exit do. I'm also used to being able to jump back and
begin the loop again. Not sure which language my...
|
by: Claudio Grondi |
last post by:
Sometimes it is known in advance, that the time spent in a loop will be
in order of minutes or even hours, so it makes sense to optimize each
element in the loop to make it run faster.
One of...
|
by: ADezii |
last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |