本文介绍了Python 2.X中的range和xrange函数之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
显然xrange更快,但是我不知道为什么它更快(到目前为止,除了轶事之外,没有证据证明它更快)
Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about
for i in range(0, 20):
for i in xrange(0, 20):
推荐答案
在Python 2.x中:
In Python 2.x:
-
range
创建一个列表,因此,如果执行range(1, 10000000)
,它将在内存中创建带有9999999
元素的列表.
range
creates a list, so if you dorange(1, 10000000)
it creates a list in memory with9999999
elements.
xrange
是一个懒惰求值的序列对象.
xrange
is a sequence object that evaluates lazily.
在Python 3中,range
等效于python的xrange
,要获取列表,您必须使用list(range(...))
.
In Python 3, range
does the equivalent of python's xrange
, and to get the list, you have to use list(range(...))
.
这篇关于Python 2.X中的range和xrange函数之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!