[Tutor] floating ranges again
alan.gauld@bt.com
alan.gauld@bt.com
Thu, 24 May 2001 10:41:21 +0100
Sorry I deleted the original thread message but
was playing around with a general purpose floating point
range function and cxame up with the following. It works
for ints or floats. You give it an initial value, the
number of entries you want(which could be the difference
of upper and lower) and the step value:
>>> def frange(first, number, step):
... return map(lambda n, f=first, s=step: f+(n*s),
... range(number))
...
>>> frange(1,10,1)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> frange(5,5,2)
[5, 7, 9, 11, 13]
>>> frange(3,10,2.5)
[3.0, 5.5, 8.0, 10.5, 13.0, 15.5, 18.0, 20.5, 23.0, 25.5]
>>> frange(5,7,4.5)
[5.0, 9.5, 14.0, 18.5, 23.0, 27.5, 32.0]
>>> frange(2.5,(7.5-2.5),1)
[2.5, 3.5, 4.5, 5.5, 6.5]
>>>
Sorry, could resist playing with that one :-)
Alan G