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
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
相关文章推荐
- 【Error处理】ABAP中调用Function Module出现“not begin with Function-Pool”错误
- Writing a sort comparison function
- time stamp log function test with variable argument
- Writing a simple Composite Component with JSF 2.0
- how to print function backtrack with boost
- jdbcTemplate异常:like模糊查询报错(Parameter index out of range (1 > number of parameters)
- CXF(2.7.10) - Writing a service with Spring
- T-SQL with as 的用法(转) SQL 下的递归查询 SQL2005(CTE) ,SQL2000(Function 递归)
- with和有名function那些不得不说的事....
- Use of deleted function error with Boost
- Writing web apps with Go, Nginx and FastCGI
- Writing IEEE articles in Chinese with TeX
- Writing ASP.NET Web Forms with C#
- stackoverflow about python dynameic inovke function with it's name
- No way to generate HTML markup with data-link and helper functions in JsViews like in JsRender
- Writing Japanese with Ubuntu 14.04 Trusty Tahr
- Implement Phonetic ("Sounds-like") Name Searches with Double Metaphone Part V: .NET Implementation
- Is it possible to implement a Firebug-like “inspect element” DOM element highlighter with client-side JavaScript?
- error LNK2019: unresolved external symbol __imp__strnicmp referenced in function _isWithBlack_me@12
- The function getXX must be used with a prefix when a default namespace is not specified