码迷,mamicode.com
首页 > 其他好文 > 详细

xrange() 与 range() 的对比与总结

时间:2015-08-08 22:46:41      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

一,两个函数的文档:

1,xrange():

xrange(stop)
xrange(start, stop[, step])

This function is very similar to range(), but returns an xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with break). For more information on xrange objects, see XRange Type and Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange.

CPython implementation detail: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (“short” Python integers), and also requires that the number of elements fit in a native C long. If a larger range is needed, an alternate version can be crafted using the itertools module: islice(count(start, step), (stop-start+step-1+2*(step<0))//step).

—————————————————————————————————————————————

xrange(stop)
xrange(start, stop[, step])

这个函数和 range() 函数很相似, 但是返回一个 xrange 对象 而不是一个list.

xrange是一个不透明的数组,它产生的值和相对应的list相同,但是xrange并不储存这些值。除了某些特定的情况(如:数组过大且内存不足;频繁的终止导致数组中的所有元素都未被访问),xrange()相比于range()并没有明显的优势。这是因为当数组中的元素被访问时,xrange()仍然需要去创建他们。

关于xrange()的更多信息,请参见 XRange Type 以及Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange.

CPython 实现细节: 为了使xrange()简单而快速,编译器会用一些严格的限制来达到这一目的。CPython将所有参数限制为long,而且要求元素个数不超过long的范围。如果需要一个更大的数组,你可以使用一个更精致的,使用了循环器(itertools)的模型:

islice(count(start, step), (stop-start+step-1+2*(step<0))//step).

2,range():

range(stop)
range(start, stop[, step])

This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not

be zero (or else ValueError is raised).

Example:

>>>

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(0, -10, -1)
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]

—————————————————————————————————————————————

 

range(stop)
range(start, stop[, step])

 

这个多功能的函数可以创建算术累加的list,所以经常被用在循环中。这个函数的参数必须是整数。Step参数的默认值是1;start参数的默认值是0。函数会返回一个整数list [start, start + step, start + 2 * step, ...]。如果stop参数为正,list中的最后一个元素回接近但不大于它;如果stop参数为负,list中的最后一个元素回接近但不小于它。Stop参数不能为0(如果为0,将会报错:ValueError)。

二,用法及总结

 

(未完待续)

xrange() 与 range() 的对比与总结

标签:

原文地址:http://www.cnblogs.com/cuixiaochen/p/4713982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!