Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three values for a for loop

I would like to have three values increment at different speeds. My overall goal is to emulate this pattern:

0,0,   0
0,1,   1
0,2,   2
1,0,   3
1,1,   4
1,2,   5
2,0,   6
2,1,   7
2,2,   8

The first two numbers are easy. I would solve it like this:

for x in range(3):
    for y in range(3):
        print(x, y)
>>> 0 0
>>> 0 1
>>> 0 2
>>> 1 0
>>> 1 1
>>> 1 2
>>> 2 0
>>> 2 1
>>> 2 2

This is the pattern that I want.

The question is how do I increment the third number by one each time, while still having the first two numbers increment in the way that they are?

Basically, how can I make the third number increment by one each time the for loop goes?

like image 455
Matt X Avatar asked Jun 28 '18 21:06

Matt X


1 Answers

Since we have all these answers, I will post the most straightforward one

count = 0
for x in range(3):
    for y in range(3):
        print(x, y, count)
        count += 1
like image 58
rafaelc Avatar answered Sep 25 '22 04:09

rafaelc