I assume this is the way for loops are written in C, but if it helps to
be specific, I'm referring to C# for loops. The Python for loop seems to
be the same (or similar) to C#'s foreach loop:
foreach int i in X
But how would you write a C# for loop in Python? Do you rework a while
loop, or use the range() function?
Here's an example:
for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way?
Thanks. 9 3196
John Salerno wrote: I assume this is the way for loops are written in C, but if it helps to be specific, I'm referring to C# for loops. The Python for loop seems to be the same (or similar) to C#'s foreach loop:
foreach int i in X
But how would you write a C# for loop in Python? Do you rework a while loop, or use the range() function?
Here's an example:
for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way?
Thanks.
Take a look at the range() builtin. That should give you what you need.
John Salerno wrote: for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way?
for i in xrange(0,50,5): print i
John Salerno <jo******@NOSPAMgmail.com> wrote: I assume this is the way for loops are written in C, but if it helps to be specific, I'm referring to C# for loops. The Python for loop seems to be the same (or similar) to C#'s foreach loop:
foreach int i in X
But how would you write a C# for loop in Python? Do you rework a while loop, or use the range() function?
Here's an example:
for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way?
for i in range(0,50,5):
print i
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Ciao, John Salerno! Che stavi dicendo? for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way?
i=0
while i<50:
#...
i+=5
about range()/xrange(): what if you want to traslate this c-loop?
for (int i=1; i<50; i*=2)
--
Evangelion e' la storia yaoi di un angelo che vuole portarsi a letto un
ragazzo che si intreccia con la storia di un maturo professionista con il
complesso delle lolite... fatte in casa.
-- Lennier, 2006
On Wed, 15 Feb 2006 10:20:13 +0000, ZeD wrote: Ciao, John Salerno! Che stavi dicendo?
for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way? i=0 while i<50: #... i+=5
That's exceedingly unPythonic. In fact I'd go far to say it is bad
practice in just about any programming language that has for loops. Why on
earth would any sensible programmer want to manage the loop variable by
hand if the language can do it for you?
about range()/xrange(): what if you want to traslate this c-loop? for (int i=1; i<50; i*=2)
That's a completely different question, so of course it has a completely
different answer. Here is one way:
for i in [2**n for n in range(6)]:
do_something(i)
Here is another:
for i in range(int(math.log(50)/math.log(2)+1)):
do_something(2**i)
Here is a third way:
i = 1
while i < 50:
do_something(i)
i *= 2
Here is a fourth way:
import operator
def looper(start, (op, finish), (op2, x)):
n = start
while op(n, finish):
yield n
n = op2(n, x)
loop = looper(1, (operator.__lt__, 50), (operator.__mul__, 2))
for i in loop:
do_something(i)
--
Steven.
Steven D'Aprano wrote: about range()/xrange(): what if you want to traslate this c-loop? for (int i=1; i<50; i*=2)
That's a completely different question, so of course it has a completely different answer. Here is one way:
.... various options snipped ...
and another way for use when you have more than one loop following this
pattern:
def powerrange(base, start=0, limit=0):
value = base**start
while value < limit:
yield value
value *= base
for i in powerrange(2,0,50):
dosomething(i)
Putting the question the other way round: how would you move the control
logic out of the loop in C?
Tim Roberts wrote: John Salerno <jo******@NOSPAMgmail.com> wrote: I assume this is the way for loops are written in C, but if it helps to be specific, I'm referring to C# for loops. The Python for loop seems to be the same (or similar) to C#'s foreach loop:
foreach int i in X
But how would you write a C# for loop in Python? Do you rework a while loop, or use the range() function?
Here's an example:
for (int i = 0; i < 50; i += 5)
How would that go in Python, in the simplest and most efficient way?
for i in range(0,50,5): print i
Thanks guys. I thought it might be something like this. In fact, the
chapter I just read dicussed how you use the range function with a for
loop, but I didn't quite see a connection with the C# for loop.
On 15 Feb 2006 12:48:11 GMT,
Duncan Booth <du**********@invalid.invalid> wrote: Putting the question the other way round: how would you move the control logic out of the loop in C?
I have written many functions not unlike this one (untested):
void for_each_record( void (*callback)( RECORD * ), void *callback_data )
{
DATA *p;
for( p = the_list; p; p = p->next )
(*callback)( p->the_record, callback_data );
return;
}
where "the_list" comes from somewhere (probably static within the module
containing for_each_record, or the loop could access a database, or
whatever), and it is up to the callers to write their own callback
functions and manage their own callback_data. Obviously, in an actual
product, there would be error checking, options for the callback
function to abort the loop early, and other bells and whistles (e.g.,
logging options) depending on the application.
But now we're off topic for comp.lang.python.
Regards,
Dan
--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Steven D'Aprano>That's a completely different question, so of course it
has a completely different answer. Here is one way:<
Other versions without the creation of a list:
for i in (2**n for n in xrange(6)):
do_something(i)
for i in (1<<n for n in xrange(6)):
do_something(i)
for i in xrange(6):
do_something(i<<i)
Bye,
bearophile This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Greg Hurlman |
last post by:
I've got what I'm sure is a very simple problem. In an ASP page, I am trying
to write out 4 fields from a recordset in succession:
Response.Write rs("LastName")
Response.Write rs("Suffix")...
|
by: Ike |
last post by:
Can anyone discern why the following code writes the document.write() lines
literally? That is, a line like document.write('<CENTER>') should write
<CENTER> but instead writes the entire ...
|
by: Ben |
last post by:
Hi all,
Just wondering how to write (using document.write) to a table cell. I
have table with 3 rows and 3 colums. I want to write from within the
Javascript to say third column of a first row....
|
by: bissatch |
last post by:
Hi,
I am trying to use JavaScript to write a table column on a web page.
The code is as follows:
<html>
<head>
<script>
function displaycount() {
|
by: Tom Van Ginneken |
last post by:
Hi,
I need to write binary data to a serial port. I am using this function:
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
I am able to write a alpha-numeric...
|
by: Just Me |
last post by:
Using streams how do I write and then read a set of variables?
For example, suppose I want to write into a text file:
string1,string2,string3
Then read them later.
Suppose I want to write...
|
by: Vmusic |
last post by:
Hi,
I am trying to write out an array of string variables to Notepad. I
can't get SendKeys to accept the string variable only literal quoted
strings.
I DO NOT want the hassle of writing to a...
|
by: Tony |
last post by:
Is it me, or is document.write just about the most abused js function?
Maybe, like goto, js would be better without it?
Is there any good reason to use it? Because I'm having a hard time
seeing...
|
by: cbtechlists |
last post by:
I have an ASP app that we've moved from a Windows 2000 to a Windows
2003 server (sql server 2000 to sql server 2005). The job runs fine on
the old servers.
Part of the app takes a recordset and...
|
by: celeroSolutions |
last post by:
This code works in my site in IE, but not in FireFox, and I'm stuck as to why! Any ideas? (The image paths are correct, I've tested these.)
<script language="javascript" type="text/javascript">...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |