I was wondering how and if it's possible to write a loop in python
which updates two or more variables at a time. For instance, something
like this in C:
for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
So that I would get:
i = 0, j = 0
i = 1, j = 1
i = 2, j = 2
....
....
....
i = 9, j = 19
Can this be done in Python?
Thanks. 3 1267 ro*********@gmail.com a écrit :
I was wondering how and if it's possible to write a loop in python
which updates two or more variables at a time. For instance, something
like this in C:
for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
So that I would get:
i = 0, j = 0
i = 1, j = 1
i = 2, j = 2
...
...
...
i = 9, j = 19
Can this be done in Python?
What's your use case exactly ? I mean, the *real* problem you're trying
to solve this way ? ro*********@gmail.com wrote:
I was wondering how and if it's possible to write a loop in python
which updates two or more variables at a time. For instance, something
like this in C:
for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
So that I would get:
i = 0, j = 0
i = 1, j = 1
i = 2, j = 2
...
...
...
i = 9, j = 19
Can this be done in Python?
Thanks.
You can zip two ranges/iterators to produce successive tuples with
values from each iterator respectively, and loop on that:
>>for i,j in zip(range(10),range(10,20)):
.... print i,j
....
0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
Gary Herron
I was wondering how and if it's possible to write a loop in python
which updates two or more variables at a time. For instance, something
like this in C:
for (i = 0, j = 10; i < 10 && j < 20; i++, j++) {
printf("i = %d, j = %d\n", i, j);
}
Well, yes it can be done, but depending on your use-case, there
might be smarter ways of doing it:
for (i,j) in map(lambda i: (i, i+10), xrange(10)):
print "i = %d, j = %d" % (i,j)
or just
for pair in map(lambda i: (i, i+10), xrange(10)):
print "i = %d, j = %d" % pair
or even just
for i in xrange(10):
print "i = %d, j = %d" % (i,i+10)
If you need varying sources, you can use zip() to do something like
for (i,j) in zip(xrange(10), myiter(72)):
print "i = %d, j = %d" % (i,j)
where myiter() produces the random sequence of items for j.
If they produce voluminous output, you can import itertools and
use izip and imap instead.
Or, if you want a more literal mapping:
i, j = 0, 10
while i < 10 && j < 20:
print "i = %d, j = %d" % (i,j)
i += 1
j += 1
Pick your poison.
So that I would get:
i = 0, j = 0
i = 1, j = 1
i = 2, j = 2
...
...
I'm not sure how, with your code, "j" could be (0,1,2,...)
instead of (10,11,12,...).
-tkc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: JustSomeGuy |
last post by:
I have a need to make an applicaiton that uses a variable number of nested
for loops.
for now I'm using a fixed number:
for (z=0; z < Z; ++z)
for (y=0; y < Y; ++y)
for (x=0; x < X; ++x)
|
by: Steven T. Hatton |
last post by:
It blows up on a 9 dimensional space, but I'm pretty sure that's due to the
size of size_t.
--- begin: DataGeneration_Test ---
Tensor<T 3, 3>...
|
by: Alf P. Steinbach |
last post by:
The third part of my attempted Correct C++ tutorial is now available,
although for now only in Word format (use free Open Office if no Word),
and also, it's not yet been extensively reviewed -- ...
|
by: Neo |
last post by:
Hi Folks,http://www.abarnett.demon.co.uk/tutorial.html#FASTFOR Page
states:for( i=0; i<10; i++){ ... }i loops through the values
0,1,2,3,4,5,6,7,8,9 If you don't care about the order of the loop...
|
by: Scott Brady Drummonds |
last post by:
Hi, everyone,
I was in a code review a couple of days ago and noticed one of my coworkers
never used for() loops. Instead, he would use while() loops such as the
following:
i = 0;
while (i...
|
by: Peter Olcott |
last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html
The above link shows that C# is 450% slower on something as simple as a nested
loop....
|
by: John Salerno |
last post by:
I'm reading Text Processing in Python right now and I came across a
comment that is helping me to see for loops in a new light. I think
because I'm used to the C-style for loop where you create a...
|
by: Sheldon |
last post by:
Hi,
I have two arrays that are of the same dimension but having 3 different
values: 255, 1 or 2.
I would like to set all the positions in both arrays having 255 to be
equal, i.e., where one...
|
by: kydavis77 |
last post by:
i was wondering if anyone could point me to some good reading about the
for and while loops
i am trying to write some programs
"Exercise 1
Write a program that continually reads in numbers...
|
by: Gandalf |
last post by:
how can I do width python a normal for loop width tree conditions like
for example :
for x=1;x<=100;x+x:
print x
thanks
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |