range,xrange,arange比较

python range介绍

range多用作循环,range(0,10)返回一个list

python xrange 介绍

xrange也是用作循环,只是xrang(0,10)不返回list,返回xrange对象。每次调用返回其中的一个值。
返回很大的数的时候或者频繁的需要break时候,xrange性能更好。

python arange

  • arange是numpy模块中的函数,使用前需要先导入此模块,arange(3):返回array类型对象。
  • doc说明:numpy.arange([start, ]stop, [step, ]dtype=None) 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.

    import numpy as np
    np.arange(2)
    array([0, 1])

range和xrange比较

>>> print range.__doc__
range(stop) -> list of integers
range(start, stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
>>> print xrange.__doc__
xrange(stop) -> xrange object
xrange(start, stop[, step]) -> xrange object
Like range(), but instead of returning a list, returns an object that
generates the numbers in the range on demand.  For looping, this is
slightly faster than range() and more memory efficient.

总结

既然两者没有区别,而且在数值大的时候xrang更优越,以后coding时候尽量用xrange。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值