>>> range(0,5) #生成一个range object,而不是[0,1,2,3,4] range(0, 5) >>> c = [i for i in range(0,5)] #从0 开始到4,不包括5,默认的间隔为1 >>> c [0, 1, 2, 3, 4] >>> c = [i for i in range(0,5,2)] #间隔设为2 >>> c [0, 2, 4]
若需要生成[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
>>> range(0,1,0.1) #range中的setp 不能使float Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> range(0,1,0.1) TypeError: 'float' object cannot be interpreted as an integer
arrange()函数
函数说明:arange([start,] stop[, step,], dtype=None)根据start与stop指定的范围以及step设定的步长,生成一个 ndarray。 dtype : dtype
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments. >>> np.arange(3) array([0, 1, 2]) >>> np.arange(3.0) array([ 0., 1., 2.]) >>> np.arange(3,7) array([3, 4, 5, 6]) >>> np.arange(3,7,2) array([3, 5])