多变量的for循环 详解
原文地址: http://blog.csdn.NET/caroline_wendy/article/details/23439661
多变量的for循环, 经常在程序中出现, 如for(int i=0, j=n-1; i<j; i++, j--) 等;
由于Python的循环机制发生改变, 所以可以使用元素对的方式(zip)的方式进行循环;
如:
[python] view plain copy print?
-- coding: utf-8 --
#eclipse pydev, python 3.3
#by C.L.Wang
#time: 2014. 4. 11
n = 9
for i, j in zip(range(n-1, 0, -1), range(n//2)):
print ('i = {0}, j = {1} '.format(i, j))
print(list(zip(range(n-1, 0, -1), range(n//2))))
输出:
[plain] view plain copy print?
i = 8, j = 0
i = 7, j = 1
i = 6, j = 2
i = 5, j = 3
[(8, 0), (7, 1), (6, 2), (5, 3)]
http://blog.csdn.net/caroline_wendy/article/details/23439661
3774

被折叠的 条评论
为什么被折叠?



