Python 中的range,以及numpy包中的arange函数
2017-10-12 14:49
453 查看
range()函数
函数说明: range(start, stop[, step]) -> range object,根据start与stop指定的范围以及step设定的步长,生成一个序列。
参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0,
5);
end:技术到end结束,但不包括end.例如:range(0,
5) 是[0, 1, 2, 3, 4]没有5
scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
函数返回的是一个range object
例子:
[python] view
plain copy
>>> 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]
[python] view
plain copy
>>> 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.
[python] view
plain copy
>>> 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])
[python] view
plain copy
>>> arange(0,1,0.1)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
函数说明: range(start, stop[, step]) -> range object,根据start与stop指定的范围以及step设定的步长,生成一个序列。
参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0,
5);
end:技术到end结束,但不包括end.例如:range(0,
5) 是[0, 1, 2, 3, 4]没有5
scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)
函数返回的是一个range object
例子:
[python] view
plain copy
>>> 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]
[python] view
plain copy
>>> 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.
[python] view
plain copy
>>> 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])
[python] view
plain copy
>>> arange(0,1,0.1)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
相关文章推荐
- Python 中的range,以及numpy包中的arange函数
- Python 中的range,以及numpy包中的arange函数
- Python 中的range,以及numpy包中的arange函数
- Python如何获得相同步长的小数数列 附range与numpy包中arange函数的用法与区别
- OpenCV与Python之图像的读入与显示以及利用Numpy的图像转换
- 在centOS上离安装Python2.7以及numpy,scipy,matplot,sklearn等
- Python:numpy中arange的使用方法
- Python中Numpy的tile函数用法以及operator.itemgetter函数和sorted函数
- win7下caffe的编译(包括caffe_python接口配置)以及python27下numpy、matplotlib、scipy等的安装
- python list定义并初始化长度 以及range()
- 数学之路-python计算实战(5)-初识numpy以及pypy下执行numpy
- python,numpy等的安装方式以及Theano的安装、vim的python开发环境配置总结
- Python中numpy.clip();numpy.fabs()的用法;以及math.pow()的说明
- IndexError: list index out of range的错误原因 和python sorted函数以及operator.itemgetter函数
- 零基础学python-12.5 修改列表的误区以及使用for和range修改列表
- 零基础学python-12.5 修改列表的误区以及使用for和range修改列表
- Linux-Ubuntu16.04下Python3.5安装pip3以及scrapy、numpy、itchat
- Python3 安装 pip3 以及 scrapy、numpy、itchat
- Python 2.7(3.x)以及numpy、matplotlib和scipy库三种方法实战安装
- Windows 7 64bit下Python以及numpy、Scipy库的安装