问题:
Recently I started using Python3 and it's lack of xrange hurts. 最近,我开始使用Python3,它缺少xrange的好处。
Simple example: 简单的例子:
1) Python2: 1) Python2:
from time import time as t
def count():
st = t()
[x for x in xrange(10000000) if x%4 == 0]
et = t()
print et-st
count()
2) Python3: 2) Python3:
from time import time as t
def xrange(x):
return iter(range(x))
def count():
st = t()
[x for x in xrange(10000000) if x%4 == 0]
et = t()
print (et-st)
count()
The results are, respectively: 结果分别是:
1) 1.53888392448 2) 3.215819835662842 1) 1.53888392448 2) 3.215819835662842
Why is that? 这是为什么? I mean, why xrange's been removed? 我的意思是,为什么xrange被删除了? It's such a great tool to learn. 这是学习的好工具。 For the beginners, just like myself, like we all were at some point. 对于初学者来说,就像我自己一样,就像我们所有人都处于某个时刻一样。 Why remove it? 为什么要删除它? Can somebody point me to the proper PEP, I can't find it. 有人可以指出我正确的PEP,我找不到它。
Cheers. 干杯。
解决方案:
参考一: https://stackoom.com/question/10zuI参考二: Why is there no xrange function in Python3?