Making a list of evenly spaced numbers in a certain range in python

158,780

Solution 1

Given numpy, you could use linspace:

Including the right endpoint (5):

In [46]: import numpy as np
In [47]: np.linspace(0,5,10)
Out[47]: 
array([ 0.        ,  0.55555556,  1.11111111,  1.66666667,  2.22222222,
        2.77777778,  3.33333333,  3.88888889,  4.44444444,  5.        ])

Excluding the right endpoint:

In [48]: np.linspace(0,5,10,endpoint=False)
Out[48]: array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])

Solution 2

You can use the following approach:

[lower + x*(upper-lower)/length for x in range(length)]

lower and/or upper must be assigned as floats for this approach to work.

Solution 3

Similar to unutbu's answer, you can use numpy's arange function, which is analog to Python's intrinsic function range. Notice that the end point is not included, as in range:

>>> import numpy as np
>>> a = np.arange(0,5, 0.5)
>>> a
array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])
>>> a = np.arange(0,5, 0.5) # returns a numpy array
>>> a
array([ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ,  3.5,  4. ,  4.5])
>>> a.tolist() # if you prefer it as a list
[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]

Solution 4

f = 0.5
a = 0
b = 9
d = [x * f for x in range(a, b)]

would be a way to do it.

Solution 5

You can use the folowing code:

def float_range(initVal, itemCount, step):
    for x in xrange(itemCount):
        yield initVal
        initVal += step

[x for x in float_range(1, 3, 0.1)]
Share:
158,780
Double AA
Author by

Double AA

Updated on July 29, 2022

Comments

  • Double AA
    Double AA almost 2 years

    What is a pythonic way of making list of arbitrary length containing evenly spaced numbers (not just whole integers) between given bounds? For instance:

    my_func(0,5,10) # ( lower_bound , upper_bound , length )
    # [ 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5 ] 
    

    Note the Range() function only deals with integers. And this:

    def my_func(low,up,leng):
        list = []
        step = (up - low) / float(leng)
        for i in range(leng):
            list.append(low)
            low = low + step
        return list
    

    seems too complicated. Any ideas?

  • unutbu
    unutbu almost 13 years
    @Double AA: True; if you need a list, you could use np.linspace(0,5,10).tolist().
  • nvd
    nvd over 9 years
    Or simply: a = N.arange(0, 5 , 0.5).tolist()
  • feedMe
    feedMe about 8 years
    Nice to have a concise stock python alternative in case numpy is not available.
  • lhcgeneva
    lhcgeneva over 7 years
    Careful when using numpy.arange(), though, endpoints are not handled consistently, giving rise to unreliable behavior, see here.
  • Trav
    Trav almost 6 years
    this updated version produces the exact same output as numpy linspace: [lower + x*(upper-lower)/(length-1) for x in range(length)
  • Bandham Manikanta
    Bandham Manikanta over 4 years
    for integers, np.arange(0, 5, 1). and for floating numbers, np.arange(0, 5, 1.0)