在 C
语言中,我们可以在单个 for
循环中使用两个索引变量,如下所示。
for (i = 0, j = 0; i < i_max && j < j_max; i++, j++)
有人能告诉我如何在 Python
中做到这一点吗?
最佳答案
使用 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
关于python - 如何在python中的单个for循环中使用2个索引变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747961/