473,395 Members | 1,972 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,395 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 1474

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
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...
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
marktang
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,...
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
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,...

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.