python2.7下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.
从上文看出range()返回一个list,而xrange()返回list中的一个数。所以,当你要在一个大的区间上迭代的时候,性能上xrange()更好,因为不需要一次性产生所有的数。但是在以下几种情况下,还是推荐range()。
- 在python3中range()做了xrange做的事情,但是xrange不存在,所以你要写在python2和3中都能运行的程序,就尽量用range()。
- range()在某些场合下会更快,比如,如果你在同一个序列上迭代很多次。xrange()每次都要重建整数对象,但是range()却不用(当然,这种方式会直接导致内存效率下降)。
- 在真正需要一个list的时候,比如需要应用list的slice特性的时候。
arange
- arange是numpy包中的一个函数,它类似range()但是并不返回list而是返回numpy.ndarray对象。而且,它不仅限于整数,但是当不是整数的时候用linspace更好,因为arange产生的结果往往会因为浮点数溢出问题而不一致。
>>>print np.arange.__doc__
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 includingstart
but excludingstop
).For integer arguments the function is equivalent to the Python built-in
range <http://docs.python.org/lib/built-in-funcs.html>
_ function, but returns an ndarray rather than a list.When using a non-integer step, such as 0.1, the results will often notbe consistent. It is better to uselinspace
for these cases.
Parameters
start : number, optional
Start of interval. The interval includes this value. The default
start value is 0.
stop : number
End of interval. The interval does not include this value, except in some cases wherestep
is not an integer and floating point round-off affects the length ofout
.
step : number, optional
Spacing between values. For any outputout
, this is the distance between two adjacent values,out[i+1] - out[i]
. The default
step size is 1. Ifstep
is specified,start
must also be given.
dtype : dtype
The type of the output array. Ifdtype
is not given, infer the data type from the other input arguments.
Returns
arange : ndarray
Array of evenly spaced values.
For floating point arguments, the length of the result is
ceil((stop - start)/step)
. Because of floating point overflow,
this rule may result in the last element ofout
being greater
thanstop
.
See Also
linspace : Evenly spaced numbers with careful handling of endpoints.
ogrid: Arrays of evenly spaced numbers in N-dimensions.
mgrid: Grid-shaped arrays of evenly spaced numbers in N-dimensions.
Examples
>>> 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])
参考文献:
http://blog.youkuaiyun.com/duankaifei/article/details/43866849
http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range