irange()与range()或xrange()有何不同? [英] How is irange() any different from range() or xrange()?

查看:199
本文介绍了irange()与range()或xrange()有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到 RangeGenerator 页面时,我正在浏览Python生成器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 range()类型序列类型,它们支持其他序列也支持的各种操作,例如报告其长度,进行遏制测试和索引编制:

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()函数进行迭代可以有效地反向

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

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

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

这篇关于irange()与range()或xrange()有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆