在python中,在一定范围内创建一个均匀间隔的数字列表

时间:2021-03-04 21:38:48

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:

注意Range()函数只处理整数。这:

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?

似乎太复杂。什么好主意吗?

6 个解决方案

#1


42  

Given numpy, you could use linspace:

给定numpy,可以使用linspace:

Including the right endpoint (5):

包括正确的端点(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])

#2


23  

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.

下端和/或上端必须作为该方法的浮点数分配。

#3


5  

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:

与unutbu的答案类似,您可以使用numpy的arange函数,它类似于Python的内部函数范围。注意,结束点不包括在内,如在范围内:

>>> 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]

#4


1  

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

would be a way to do it.

这是一种方法。

#5


1  

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)]

#6


0  

Similar to Howard's answer but a bit more efficient:

类似于霍华德的回答,但更有效率一点:

def my_func(low, up, leng):
    step = ((up-low) * 1.0 / leng)
    return [low+i*step for i in xrange(leng)]

#1


42  

Given numpy, you could use linspace:

给定numpy,可以使用linspace:

Including the right endpoint (5):

包括正确的端点(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])

#2


23  

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.

下端和/或上端必须作为该方法的浮点数分配。

#3


5  

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:

与unutbu的答案类似,您可以使用numpy的arange函数,它类似于Python的内部函数范围。注意,结束点不包括在内,如在范围内:

>>> 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]

#4


1  

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

would be a way to do it.

这是一种方法。

#5


1  

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)]

#6


0  

Similar to Howard's answer but a bit more efficient:

类似于霍华德的回答,但更有效率一点:

def my_func(low, up, leng):
    step = ((up-low) * 1.0 / leng)
    return [low+i*step for i in xrange(leng)]