473,320 Members | 1,883 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,320 software developers and data experts.

Re: Multiple variable control in for loops. Doable in Python?

On Fri, Jul 18, 2008 at 12:21:49PM -0700, mark floyd wrote:
I'm new to Python and have been doing work converting a few apps
from Perl to Python. I can not figure out the comparable Python
structures for multi-variable for loop control.
[...]
I spent a good part of yesterday looking for a way to handle this
style for loop in Python and haven't been able to find an
appropriate way to handle this control style.
One wonders why... :)
We have this style for loop all over the place and not being able to
find a similar structure in Python could be a problem. Any pointers
to a Python equivalent structure would be much appreciated
Even if Python didn't offer a way to write a for loop in a similar
fashion (someone else replied about that already), why should it be a
problem? In general control structures can be rewritten as some other
kind of control structure. For example, this does exactly what your
for loop examples do:

i = 0
j = 0
while i < 5 and j < 10:
print i, j
i += 1
j += 1

Though, this example is silly, as it will always terminate after the
5th iteration of the loop, and there is no need to have j being used
as a control variable... it's termination condition will never be met.
Though the example illustrates the techique, even if the example is
bogus.

Another way is to use functions to modify the values of i and j.
Writing your loops this way, you can have as many control variables as
you need, and your formula for incrementing those control variables
can be as varied as complicated as you can imagine.

def some_increment_function(i):
# Do some complicated processing of i
i = ...
return i

def other_incrjmental_function(j):
# Do some complicated processing of j
j = ...
return j

i = 0
j = 0
while i < 5 and j < 10:
print i, j
i = some_increment_function(i)
j = other_increment_function(j)

And of course, you could also replace the loop terminating conditions
with functions that return a value which can be interpreted as a truth
value. If you wanted to get really crazy, you could even code the
control structure as a recursive function:

def control(i, j):
print i,j
if not (i < 5 or j < 10):
return
else:
control(some_increment_function(i), other_increment_function(j))
Should you really write control structures this way, generally?
Absolutely not (unless you're writing LISP or Scheme :)). But the
point is, even if a given language doesn't have a particular syntactic
element that you're looking for, it's pretty much guaranteed to
provide a way to do what you're trying to do. You just need to stop
thinking about your problem in terms of a particular syntactic
element, and start thinking about it in more general terms of what you
are actually trying to accomplish, and apply whatever syntax (usually
one of several) your language provides to do that.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFIgQsAHEnASN++rQIRAlx2AKCGgS2pBb3csYid7H0pgM W0vBYUUQCeJ1rB
lRjw9yBf1UXMLZRU3zn1Irw=
=CVNL
-----END PGP SIGNATURE-----

Jul 18 '08 #1
0 1470

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
32
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
6
by: Wijaya Edward | last post by:
Can we make loops control in Python? What I mean is that whether we can control which loops to exit/skip at the given scope. For example in Perl we can do something like: OUT: foreach my $s1...
37
by: bahoo | last post by:
Hi, I have a list like and as output I want If I myList.remove('0024') then only the first instance of '0024' is removed.
2
by: bdude | last post by:
Hey, I'm new to python and am looking for the most efficient way to see if the contents of a variable is equal to one of many options. Cheers, Bryce R
6
by: nullgraph | last post by:
Hi everyone, I'm new to Python and the notion of lambda, and I'm trying to write a function that would have a varying number of nested for loops depending on parameter n. This just smells like a...
13
by: Joel Koltner | last post by:
Is there an easy way to get a list comprehension to produce a flat list of, say, for each input argument? E.g., I'd like to do something like: for x in range(4) ] ....and receive
0
by: Eduardo O. Padoan | last post by:
On Fri, Jul 18, 2008 at 4:21 PM, mark floyd <emfloyd2@gmail.comwrote: Ops, sorry, sent only to Mark. Here is the asnwer again: for i, j in zip(range(0, I_MAX, 5), range(0, J_MAX, 10)):...
5
by: TP | last post by:
Hi everybody, Several means to escape a nested loop are given here: http://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops-in-python According to this page, the best...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
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...
0
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...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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 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.