python-3.xlistcontrolsdiscrete-mathematicsdiscretization

List between 2 ranges and n elements in python


How can i create a list of numbers that it will be a serie? for example function(0, 2, 5) from 0 to 2 with 5 elements --> [0, 0.5, 1, 1.5, 2] Is there any function in python which can do it?


Solution

  • This is a simple function that calculates the delta and create an array.

    def n_spaced_range(x_start, x_end, n_elements):
        d = (x_end - x_start) / (n_elements-1)
        return [x_start + i*d for i in range(n_elements)]