如何在python的单个for循环中使用2个索引变量

ras*_*hok 5 python for-loop

C语言中,我们可以在一个for循环中使用两个索引变量,如下所示。

for (i = 0, j = 0; i < i_max && j < j_max; i++, j++)
Run Code Online (Sandbox Code Playgroud)

有人可以告诉如何做到这一点Python吗?

ras*_*hok 8

有了zip我们就可以实现这一点。

>>> i_max = 5
>>> j_max = 7
>>> for i, j in zip(range(0, i_max), range(0, j_max)):
...     print str(i) + ", " + str(j)
...
0, 0
1, 1
2, 2
3, 3
4, 4
Run Code Online (Sandbox Code Playgroud)