python-small-examples

@author jackzhenguo
@desc 
@date 2019/4/14

71 浮点数等差数列

def float_range(start, stop, n):
    start,stop,n = float('%.2f' % start), float('%.2f' % stop),int('%.d' % n)
    step = (stop-start)/n
    lst = [start]
    while n > 0:
        start,n = start+step,n-1
        lst.append(round((start), 2))
    return lst

测试举例:

float_range(1, 8, 10) 
# [1.0, 1.7, 2.4, 3.1, 3.8, 4.5, 5.2, 5.9, 6.6, 7.3, 8.0]
[上一个例子](/python-small-examples/md/70.html) [下一个例子](/python-small-examples/md/72.html)