您的位置:首页 > 其它

Recipe 19.1. Writing a range-like Function with Float Increments

2009-10-29 22:50 232 查看
frange
1 >>> import itertools
2 >>> def frange(start, end=None, inc=1.0):
3 if end is None:
4 end = start + 0.0
5 start = 0.0
6 assert inc
7 for i in itertools.count():
8 next = start + i * inc
9 if(inc>0.0 and next >= end) or (inc<0.0 and next <= end):
10 break
11 yield next
12

1 >>> for j in frange(1.0, 5.0, 0.5):
2 print(j)
Outputs:

1.0
1.5
2.0
2.5
3.0
3.5
4.0
4.5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: