python 获取等间隔的数组

可以使用numpy中的linspace函数

np.linspace(start, stop, num, endpoint, retstep, dtype)
#start和stop为起始和终止位置,均为标量
#num为包括start和stop的间隔点总数,默认为50
#endpoint为bool值,为False时将会去掉最后一个点计算间隔
#restep为bool值,为True时会同时返回数据列表和间隔值
#dtype默认为输入变量的类型,给定类型后将会把生成的数组类型转为目标类型
np.linspace(1,3,num=4)
Out[17]: array([1.        , 1.66666667, 2.33333333, 3.        ])

np.linspace(1,3,num=4,endpoint=False)
Out[18]: array([1. , 1.5, 2. , 2.5])

np.linspace(1,3,num=4,endpoint=False,retstep=True)
Out[19]: (array([1. , 1.5, 2. , 2.5]), 0.5)

np.linspace(1,3,num=4,endpoint=False,retstep=True,dtype=float)
Out[20]: (array([1. , 1.5, 2. , 2.5]), 0.5)

np.linspace(1,3,num=4,endpoint=False,retstep=True,dtype=int)
Out[21]: (array([1, 1, 2, 2]), 0.5)

猜你喜欢

转载自blog.csdn.net/z962013489/article/details/82497361
今日推荐