没有合适的资源?快使用搜索试试~ 我知道了~
首页浅谈Python中range与Numpy中arange的比较
浅谈Python中range与Numpy中arange的比较
3 下载量 179 浏览量
更新于2023-03-16
2
收藏 62KB PDF 举报
本文先比较range与arange的异同点,再详细介绍各自的用法,然后列举了几个简单的示例,最后对xrange进行了简单的说明。 1. range与arange的比较 (1)相同点:A、参数的可选性、默认缺省值是一样的;B、结果均包括开始值,不包括结束值; C、arange的参数为整数是,与range函数等价;D、都具备索引查找、要素截取等操作。 (2)不同点:A、range函数的参数只能为整数,arange的参数为数值型,包括整数和浮点数; B、输出的数据类型不同,range的输出为列表(list),arange的结果为数组(ndarray);C、range的最后一个元素,不会超过stop,
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/13737569/bg1.jpg)
浅谈浅谈Python中中range与与Numpy中中arange的比较的比较
本文先比较range与arange的异同点,再详细介绍各自的用法,然后列举了几个简单的示例,最后对xrange进行了简单的说
明。
1. range与与arange的比较的比较
(1)相同点:A、参数的可选性、默认缺省值是一样的;B、结果均包括开始值,不包括结束值; C、arange的参数为整数
是,与range函数等价;D、都具备索引查找、要素截取等操作。
(2)不同点:A、range函数的参数只能为整数,arange的参数为数值型,包括整数和浮点数; B、输出的数据类型不
同,range的输出为列表(list),arange的结果为数组(ndarray);C、range的最后一个元素,不会超过stop,而arange的最后一
个元素可能会比stop的值大。
2. python中的范围中的范围range
(1)官方文档对range的定义为:The range type represents an immutable sequence of numbers and is commonly used for
looping a specific number of times in for loops.(range代表不可变的数字序列,通常用于一定次数的循环中)。
若步长为正数,范围r的值由公式r[i] = start + step*i确定,限制条件为i >=0、r[i] < stop;
若步长为负数,范围r的值同样由公式r[i] = start + step*i确定,但限制条件为i >=0、r[i] > stop;
(2)参数说明
range(start, stop[, step])
start:序列的开始值,如果缺省默认为0;
stop:序列的结束值;
step:序列的步长,缺省的默认值为1。
range中所有的参数必须为整数,必须为整数!
(3)range的其他说明
A、range相比常规list或tuple的优势在于,不管range代表的范围有多大,它实际上只存储范围的开始、结束和步长值,它占
用相同且很小的内存空间;
B、range继承于collections.abc.Sequence,提供诸如包含内容是否相等的比较、索引查找、要素截取、支持负数索引等。
3. Numpy中的中的arange
(1)官方文档中的定义:Return evenly spaced values within a given interval.(返回给定间距内返回均匀间隔的值)
Values are generated within the half-open interval [start, stop) (in other words, the interval including start but excluding
stop). For integer arguments the function is equivalent to the Python built-in range function, but returns an ndarray rather
than a list.(值的范围在半开放的间隔[start, dtop)内,也就是包括start起始值,不包括stop结束值;若参数均为整数,与
python中的range函数等价,但是它返回的是数组而非列表)When using a non-integer step, such as 0.1, the results will
often not be consistent. It is better to use linspace for these cases.(当使用非整数步长时,比如0.1,结果经常不是一致的,
在这种情况下,最好使用线性等分向量)。
(2)参数说明
numpy.range([start,] stop, [step,] dtype=None)
start:数字型,可选参数,间隔的开始值,间隔包括开始值,缺省时的默认值是0;
stop:数字型,必填参数,间隔的结束值,间隔不包括结束值,除非一些特殊情况,比如步长不是整数,浮点数的四舍五入影
响到输出的长度;
step:数组型,可选参数,间距值,对任何输出,它是相邻两个值之间的差值,out[i+1] – out[i],缺省的默认值为1,如果指定
间距值,开始值也必须指定。
dtype:输出数组的类型,如果没有指定,从输入参数的类型推断输出结果的数据类型(即与输入参数的类型保持一致)。
返回值:等间距的数组,对浮点型参数,结果的长度为 ceil((stop-start)/step),因浮点溢出,这可能导致最后一个元素大于结
束值。
4、简单的示例、简单的示例
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)