<small id='PYZzn'></small><noframes id='PYZzn'>

      <tfoot id='PYZzn'></tfoot>

      <i id='PYZzn'><tr id='PYZzn'><dt id='PYZzn'><q id='PYZzn'><span id='PYZzn'><b id='PYZzn'><form id='PYZzn'><ins id='PYZzn'></ins><ul id='PYZzn'></ul><sub id='PYZzn'></sub></form><legend id='PYZzn'></legend><bdo id='PYZzn'><pre id='PYZzn'><center id='PYZzn'></center></pre></bdo></b><th id='PYZzn'></th></span></q></dt></tr></i><div id='PYZzn'><tfoot id='PYZzn'></tfoot><dl id='PYZzn'><fieldset id='PYZzn'></fieldset></dl></div>

    1. <legend id='PYZzn'><style id='PYZzn'><dir id='PYZzn'><q id='PYZzn'></q></dir></style></legend>
      • <bdo id='PYZzn'></bdo><ul id='PYZzn'></ul>
      1. irange() 与 range() 或 xrange() 有何不同?

        How is irange() any different from range() or xrange()?(irange() 与 range() 或 xrange() 有何不同?)
            <tbody id='Ki8u8'></tbody>

            <bdo id='Ki8u8'></bdo><ul id='Ki8u8'></ul>
            <tfoot id='Ki8u8'></tfoot>

            1. <small id='Ki8u8'></small><noframes id='Ki8u8'>

              <i id='Ki8u8'><tr id='Ki8u8'><dt id='Ki8u8'><q id='Ki8u8'><span id='Ki8u8'><b id='Ki8u8'><form id='Ki8u8'><ins id='Ki8u8'></ins><ul id='Ki8u8'></ul><sub id='Ki8u8'></sub></form><legend id='Ki8u8'></legend><bdo id='Ki8u8'><pre id='Ki8u8'><center id='Ki8u8'></center></pre></bdo></b><th id='Ki8u8'></th></span></q></dt></tr></i><div id='Ki8u8'><tfoot id='Ki8u8'></tfoot><dl id='Ki8u8'><fieldset id='Ki8u8'></fieldset></dl></div>

            2. <legend id='Ki8u8'><style id='Ki8u8'><dir id='Ki8u8'><q id='Ki8u8'></q></dir></style></legend>

                • 本文介绍了irange() 与 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
                  

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)

                  <small id='PY22j'></small><noframes id='PY22j'>

                  <i id='PY22j'><tr id='PY22j'><dt id='PY22j'><q id='PY22j'><span id='PY22j'><b id='PY22j'><form id='PY22j'><ins id='PY22j'></ins><ul id='PY22j'></ul><sub id='PY22j'></sub></form><legend id='PY22j'></legend><bdo id='PY22j'><pre id='PY22j'><center id='PY22j'></center></pre></bdo></b><th id='PY22j'></th></span></q></dt></tr></i><div id='PY22j'><tfoot id='PY22j'></tfoot><dl id='PY22j'><fieldset id='PY22j'></fieldset></dl></div>
                      • <bdo id='PY22j'></bdo><ul id='PY22j'></ul>
                      • <tfoot id='PY22j'></tfoot>

                            <tbody id='PY22j'></tbody>
                        1. <legend id='PY22j'><style id='PY22j'><dir id='PY22j'><q id='PY22j'></q></dir></style></legend>