#python中的range函数支持步进,如下:
>>> print range(2,15,3)
[2, 5, 8, 11, 14]
#但是浮点数不支持range函数,自己定义一个类似的
def floatrange(start,stop,steps):
''' Computes a range of floating value.
Input:
start (float) : Start value.
end (float) : End value
steps (integer): Number of values
Output:
A list of floats
Example:
>>> print floatrange(0.25, 1.3, 5)
[0.25, 0.51249999999999996, 0.77500000000000002, 1.0375000000000001, 1.3]
'''
return [start+float(i)*(stop-start)/(float(steps)-1) for i in range(steps)]
#运行范例:
>>> print floatrange(0.25, 1.3, 5)
[0.25, 0.51249999999999996, 0.77500000000000002, 1.0375000000000001, 1.3]
#//python/1866