473,412 Members | 5,714 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,412 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 1476

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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
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.