[Tutor] range()-like function for dealing with floats...?
Eike Welk
eike.welk at gmx.net
Fri Mar 30 14:14:43 CEST 2007
On Friday 30 March 2007 11:17, Joydeep Mitra wrote:
> Hi all,
> I need to generate a sequence of real numbers that are evenly
> spaced. I have used loops for this purpose until now, but it gets
> kinna messy when you gotta do it a number of times in a program.
> The range function, as per my knowledge, accepts parameters and
> return values only as integers.
> Is there a function in python, which performs the task of the
> range() function with floats?
>
> Joy
The linspace function from the NumPy library (http://numpy.scipy.org/)
does exactly what you want:
In [1]:from numpy import *
In [2]:linspace(1, 1.1, 4)
Out[2]:array([ 1. , 1.03333333, 1.06666667, 1.1 ])
If you do a lot of computations with sequences of floating point
numbers, you might consider installing this libraray.
Alternatively you could write a similar function that returns such a
sequence. (Excuse me if this is trivial for you.):
def myLinspace(xMin, xMax, nX):
deltaX = (xMax - xMin)/(nX-1)
x = [xMin + deltaX*i for i in range(nX)]
return x
Regards, Eike.
--
Snail: Phone:
Eike Welk xx49-241-4127686
Stolberger Str. 72
52068 Aachen Mail:
Germany eike.welk at post.rwth-aachen.de
More information about the Tutor
mailing list