Python - 多变量的for循环 详解

多变量的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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值