为什么或者为什么不呢?


当前回答

不,它们都有自己的用途:

迭代时使用xrange(),因为它节省内存。说:

for x in xrange(1, one_zillion):

而不是:

for x in range(1, one_zillion):

另一方面,如果实际需要一个数字列表,则使用range()。

multiples_of_seven = range(7,100,7)
print "Multiples of seven < 100: ", multiples_of_seven

其他回答

对于性能而言,尤其是在大范围内迭代时,xrange()通常更好。然而,仍然有一些情况下你可能更喜欢range():

In python 3, range() does what xrange() used to do and xrange() does not exist. If you want to write code that will run on both Python 2 and Python 3, you can't use xrange(). range() can actually be faster in some cases - eg. if iterating over the same sequence multiple times. xrange() has to reconstruct the integer object every time, but range() will have real integer objects. (It will always perform worse in terms of memory however) xrange() isn't usable in all cases where a real list is needed. For instance, it doesn't support slices, or any list methods.

[编辑]有几篇文章提到了如何通过2to3工具升级range()。为了记录,下面是在range()和xrange()的一些示例用法上运行该工具的输出。

RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: ws_comma
--- range_test.py (original)
+++ range_test.py (refactored)
@@ -1,7 +1,7 @@

 for x in range(20):
-    a=range(20)
+    a=list(range(20))
     b=list(range(20))
     c=[x for x in range(20)]
     d=(x for x in range(20))
-    e=xrange(20)
+    e=range(20)

如您所见,当在for循环或推导式中使用时,或者已经使用list()包装时,range保持不变。

Range (): Range(1,10)返回1到10个数字的列表,并将整个列表保存在内存中。 xrange():类似于range(),但不是返回一个列表,而是返回一个根据需要生成范围内数字的对象。对于循环,这比range()略快,内存效率更高。xrange()对象类似于迭代器,并根据需要生成数字(惰性求值)。

In [1]: range(1,10)
Out[1]: [1, 2, 3, 4, 5, 6, 7, 8, 9]

In [2]: xrange(10)
Out[2]: xrange(10)

In [3]: print xrange.__doc__
Out[3]: xrange([start,] stop[, step]) -> xrange object

range()所做的事情与Python 3中xrange()所做的事情相同,并且Python 3中不存在术语xrange()。 如果多次迭代同一个序列,那么Range()实际上在某些场景中更快。Xrange()每次都必须重构整数对象,但range()将拥有真正的整数对象。

虽然在大多数情况下xrange比range快,但性能上的差异非常小。下面的小程序比较了range和xrange的迭代:

import timeit
# Try various list sizes.
for list_len in [1, 10, 100, 1000, 10000, 100000, 1000000]:
  # Time doing a range and an xrange.
  rtime = timeit.timeit('a=0;\nfor n in range(%d): a += n'%list_len, number=1000)
  xrtime = timeit.timeit('a=0;\nfor n in xrange(%d): a += n'%list_len, number=1000)
  # Print the result
  print "Loop list of len %d: range=%.4f, xrange=%.4f"%(list_len, rtime, xrtime)

下面的结果显示xrange确实更快,但还不足以让人担心。

Loop list of len 1: range=0.0003, xrange=0.0003
Loop list of len 10: range=0.0013, xrange=0.0011
Loop list of len 100: range=0.0068, xrange=0.0034
Loop list of len 1000: range=0.0609, xrange=0.0438
Loop list of len 10000: range=0.5527, xrange=0.5266
Loop list of len 100000: range=10.1666, xrange=7.8481
Loop list of len 1000000: range=168.3425, xrange=155.8719

所以无论如何都要使用xrange,但除非您在受限的硬件上,否则不要太担心它。

选择范围有以下几个原因:

1) xrange将在新的Python版本中消失。这为您提供了方便的未来兼容性。

2) range将具有与xrange相关的效率。

You should favour range() over xrange() only when you need an actual list. For instance, when you want to modify the list returned by range(), or when you wish to slice it. For iteration or even just normal indexing, xrange() will work fine (and usually much more efficiently). There is a point where range() is a bit faster than xrange() for very small lists, but depending on your hardware and various other details, the break-even can be at a result of length 1 or 2; not something to worry about. Prefer xrange().