473,854 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_incrjment al_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_in crement_functio n(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)

iD8DBQFIgQsAHEn ASN++rQIRAlx2AK CGgS2pBb3csYid7 H0pgMW0vBYUUQCe J1rB
lRjw9yBf1UXMLZR U3zn1Irw=
=CVNL
-----END PGP SIGNATURE-----

Jul 18 '08 #1
0 1503

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

Similar topics

83
6557
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
32
3830
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
1576
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 ( 0 ...100) { IN:
37
19223
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
1080
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
1198
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 job for lambda for me, but I can't figure out how to do it. Any hint? For example, for n=2, I want the function to look something like: def foo(2)
13
1529
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
253
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)): do_stuff(...) --
5
1747
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 way is to modify the loop by affecting the variables that are tested in the loops. Otherwise, use exception: "If, for some reason, the terminating conditions can't be worked out,
0
9901
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9751
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11025
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10758
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7915
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5743
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4562
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.