python中while语句等价于for语句_Python是否有for循环等价物(不是foreach)

这将在紧急情况下起作用:def cfor(start, test_func, cycle_func):

"""A generator function that emulates the most common case of the C for

loop construct, where a variable is assigned a value at the begining, then

on each next cycle updated in some way, and exited when a condition

depending on that variable evaluates to false. This function yields what

the value would be at each iteration of the for loop.

Inputs:

start: the initial yielded value

test_func: called on the previous yielded value; if false, the

the generator raises StopIteration and the loop exits.

cycle_func: called on the previous yielded value, retuns the next

yielded value

Yields:

var: the value of the loop variable

An example:

for x in cfor(0.0, lambda x: x <= 3.0, lambda x: x + 1.0):

print x # Obviously, print(x) for Python 3

prints out

0.0

1.0

2.0

3.0

"""

var = start

while test_func(var):

yield var

var = cycle_func(var)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值