小编cat*_*sia的帖子

为什么Python3中没有xrange函数?

最近我开始使用Python3,它缺乏xrange的伤害.

简单的例子:

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()
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

结果分别是:

1) 1.53888392448 2) 3.215819835662842

这是为什么?我的意思是,为什么xrange被删除了?这是一个很好的学习工具.对于初学者,就像我一样,就像我们所有人一样.为什么删除它?有人能指出我正确的PEP,我找不到它.

干杯.

python xrange pep python-3.x

259
推荐指数
3
解决办法
32万
查看次数

标签 统计

pep ×1

python ×1

python-3.x ×1

xrange ×1