[Tutor] Loop over floating point values
Amit Saha
amitsaha.in at gmail.com
Mon Dec 2 07:35:45 CET 2013
On Sun, Dec 1, 2013 at 7:14 PM, Dominik George <nik at naturalnet.de> wrote:
> Hi,
>
>> - Do not create a list of the floating point values as i=[0.01, 0.02,
>> 0.03..] - either like that or by using a suitable mathematical formula
>> combined with a list comprehension
>
> You could simply write your own version of xrange that does it, as a
> generator:
>
> def xrange_f(start, stop, step):
> x = start
> while x < stop:
> yield x
> x += step
>
> Then, in your code, you can do:
>
> for f in xrange_f(0, 10, 0.01):
> pass
Thanks Dominik. Yes, this is a good abstraction if I need this
functionality at multiple places.
Best,
Amit.
--
http://echorand.me
More information about the Tutor
mailing list