range() 和 xrange() 是兩個函數,可用於在為了Python 中的循環。在Python3,沒有 xrange,但 range 函數的行為類似於 Python2 中的 xrange。如果您想編寫可在 Python2 和 Python3 上運行的代碼,則應使用range()。兩者都以不同的方式實現,並且具有不同的相關特征。比較的要點是:
- 返回類型
- Memory
- 操作使用
- Speed
Pythonrange()函數
range()返回給定範圍內的數字序列。它最常見的用途是使用 Python 循環對數字序列進行迭代。
Pythonxrange()函數
Python中的xrange()函數用於生成數字序列,類似於Python的range()函數。 Python xrange() 僅在 Python 2.x 中使用,而 Python 中的 range() 函數在 Python 3.x 中使用。
range() 與 xrange() 中的返回類型
這xrange()函數返回生成器對象隻能通過循環來顯示數字。唯一的特定範圍是根據需要顯示的,因此稱為“惰性評估“,而在 Python 中 range() 函數返回一個範圍對象(可迭代的類型)。
Python3
# initializing a with range()
a = range(1, 10000)
# initializing a with xrange()
x = xrange(1, 10000)
# testing the type of a
print("The return type of range() is : ")
print(type(a))
# testing the type of x
print("The return type of xrange() is : ")
print(type(x))
輸出:
The return type of range() is :
<type 'list'>
The return type of xrange() is :
<type 'xrange'>
xrange()和range()函數的速度
變量存儲的是範圍由range()創建需要更多內存與使用 xrange() 存儲範圍的變量相比。其基本原因是range()的返回類型是列表,而xrange()是xrange()對象。
Python3
import sys
# initializing a with range()
a = range(1,10000)
# initializing a with xrange()
x = xrange(1,10000)
# testing the size of a
# range() takes more memory
print ("The size allotted using range() is : ")
print (sys.getsizeof(a))
# testing the size of x
# xrange() takes less memory
print ("The size allotted using xrange() is : ")
print (sys.getsizeof(x))
輸出:
The size allotted using range() is :
80064
The size allotted using xrange() is :
40
xrange()和range()函數的操作使用
range() 返回列表,其中包含所有操作能夠可以應用到列表上就可以使用了。另一方麵,當 xrange() 返回 xrange 對象時,與列表關聯的操作不能應用於它們,因此是一個缺點。
Python3
# initializing a with range()
a = range(1,6)
# initializing a with xrange()
x = xrange(1,6)
# testing usage of slice operation on range()
# prints without error
print ("The list after slicing using range is : ")
print (a[2:5])
# testing usage of slice operation on xrange()
# raises error
print ("The list after slicing using xrange is : ")
print (x[2:5])
錯誤:
Traceback (most recent call last):
File "1f2d94c59aea6aed795b05a19e44474d.py", line 18, in
print (x[2:5])
TypeError: sequence index must be integer, not 'slice'
輸出:
The list after slicing using range is :
[3, 4, 5]
The list after slicing using xrange is :
Python 中 range() 和 xrange() 之間的區別
由於 xrange() 僅評估生成器對象,該對象僅包含延遲評估所需的值,因此是快點在實施中比range()。
要點:
- 如果您想編寫可在 Python 2 和 Python 3 上運行的代碼,請使用 range(),因為 xrange 函數在 Python 3 中已棄用。
- 如果多次迭代同一序列,range() 會更快。
- xrange()每次都必須重建整數對象,但range()將擁有真正的整數對象。 (然而,它在 memory 方麵總是表現得更差)
range() |
xrange() |
---|---|
返回整數列表。 | 返回一個生成器對象。 |
執行速度較慢 | 執行速度更快。 |
占用更多內存,因為它將整個元素列表保留在內存中。 | 占用更少的內存,因為它一次隻在內存中保留一個元素。 |
當它返回一個列表時,可以執行所有算術運算。 | 無法對xrange()執行此類操作。 |
在 python 3 中,不支持xrange()。 | 在 python 2 中,xrange() 用於在 for 循環中進行迭代。 |
相關用法
- Python range() vs xrange()用法及代碼示例
- Python range()用法及代碼示例
- Python range用法及代碼示例
- Python range構造函數用法及代碼示例
- Python random randrange()用法及代碼示例
- Python random.seed( )用法及代碼示例
- Python random.sample()用法及代碼示例
- Python randint()用法及代碼示例
- Python randrange()用法及代碼示例
- Python random.setstate()用法及代碼示例
- Python random.getstate()用法及代碼示例
- Python random.getrandbits()用法及代碼示例
- Python random.random()用法及代碼示例
- Python random.weibullvariate()用法及代碼示例
- Python random.expovariate()用法及代碼示例
- Python random.lognormvariate()用法及代碼示例
- Python random.gauss()用法及代碼示例
- Python random.normalvariate()用法及代碼示例
- Python random.paretovariate()用法及代碼示例
- Python random.vonmisesvariate()用法及代碼示例
- Python random.gammavariate()用法及代碼示例
- Python random.shuffle()用法及代碼示例
- Python random.triangular()用法及代碼示例
- Python random.betavariate()用法及代碼示例
- Python random.gammavariate用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 range() vs xrange() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。