爲什麼Python3中沒有xrange函數? - Why is there no xrange function in Python3?

問題:

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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章