irange() 与 range() 或 xrange() 有何不同?

2022-01-24 00:00:00 python generator range xrange

问题描述

当我遇到这个 RangeGenerator 页面时,我正在浏览 Python Generators Wiki,其中讨论了 <代码>irange() -

I was going through Python Generators Wiki when I came across this RangeGenerator page which talks about irange() -

这将让我们迭代大范围的数字,而无需求助于 xrange,这是一个惰性列表,而不是生成器.

This will let us iterator over large spans of numbers without resorting to xrange, which is a lazy list as opposed to a generator.

我似乎无法理解该页面上描述的测试套件和实现.我知道 range() 在内存中创建一个列表(从 Python 2.7 的角度来看),而 xrange() 是一个生成器.irange() 有什么不同?

I can't seem to understand the test suite and the implementation described on that page. I know that range() creates a list in the memory (from Python 2.7 point of view) and xrange() is a generator. How is irange() any different?


解决方案

irange() 返回一个生成器类型,只能迭代.没有其他的.一旦你迭代它,生成器就会耗尽,不能再次迭代.

irange() returns a generator type, which can only be iterated over. Nothing else. Once you iterated over it, the generator is exhausted and can not be iterated over again.

Python 2 xrange() 类型 和 Python 3 range() type 是 sequence 类型,它们支持其他序列也支持的各种操作,例如报告它们的长度,测试是否包含,和索引:

The Python 2 xrange() type and Python 3 range() type are sequence types, they support various operations that other sequences support as well, such as reporting on their length, test for containment, and indexing:

>>> xr = xrange(10, 20, 3)
>>> len(xr)
4
>>> 10 in xr
True
>>> xr[0]
10
>>> xr[1]
13

您可以多次迭代这些对象:

You can iterate over these objects more than once:

>>> for i in xr:
...     print i,
... 
10 13 16 19
>>> for i in xr:
...     print i,
... 
10 13 16 19

您甚至可以使用 reversed() 函数 高效地反向迭代它们:

You can even use the reversed() function to iterate over them in reverse, efficiently:

>>> for i in reversed(xr):
...     print i,
... 
19 16 13 10

Python 3 range() 类型是 xrange() 的改进版本,因为它支持更多的序列操作,仍然更高效,并且可以处理值超出 sys.maxint (在 Python 2 中将是 long 整数).

The Python 3 range() type is an improved version of xrange(), in that it supports more sequence operations, is more efficient still, and can handle values beyond sys.maxint (what would be a long integer in Python 2).

它支持切片,例如,切片值会生成一个 new range() 对象:

It supports slicing, for example, which results in a new range() object for the sliced values:

>>> r = range(10, 20, 3)
>>> r[:2]
range(10, 16, 3)

您可以像使用其他 Python 序列一样使用负索引来获取从末尾开始计数的元素:

You can use negative indices just like you can with other Python sequences, to get elements counting from the end:

>>> r[-2]
16
>>> r[-2:]
range(16, 22, 3)

并且该类型支持相等性测试;如果两个 range() 实例产生相同的值,则它们是相等的:

and the type supports testing for equality; two range() instances are equal if they'd yield the same values:

>>> range(10, 20, 3) == range(10, 21, 3)
True

在 Python 2 中,生成器 irange() 可能具有的唯一优势是它不受 xrange() 对非长整数的限制受到:

In Python 2, the only advantage the generator irange() might have is that it doesn't suffer from the limitation to non-long integers that xrange() is subjected to:

>>> import sys
>>> xrange(sys.maxint)
xrange(9223372036854775807)
>>> xrange(sys.maxint + 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long

相关文章