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?
Similar to Howard's answer but a bit more efficient:
You can use the folowing code:
Given numpy, you could use linspace:
Including the right endpoint (5):
Excluding the right endpoint:
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 inrange
:You can use the following approach:
lower and/or upper must be assigned as floats for this approach to work.
would be a way to do it.