Differences between using range() and xrange() in Python
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
What is the difference between range and xrange?
现在我在学Python。我发现在使用for循环时有两个表示范围的方法,分别是range()和xrange()。有人能给我一些关于这两种方法之间的区别的想法吗?对于每一种方法,适合的场景是什么?谢谢!
在这种情况下,
1 2 3 4 5 6 7 8 9 10 11 12 | % pydoc range Help on built-in function range in module __builtin__: range(...) 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. |
这里也是在线的。
对比:
1 2 3 4 5 6 7 8 9 10 | % pydoc xrange Help on class xrange in module __builtin__: class 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. |
也在网上!