python 的 range 和 xrange 之间那理不清的关系
2012-08-31 18:53
411 查看
上网搜索,可以得到以下的结论:
xrange(3): // 返回 xrange类型
range(3) : // 返回 list类型
之间的区别,官方doc:
xrange([start], stop[, step])
This function is very similar to range(), but returns an “xrange object” instead of a
list. This is an opaque sequence type which yields the same
values as the corresponding list, without actually storing
them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create
the values when asked for them) except when a very large range
is used on a memory-starved machine or when all of the range’s
elements are never used (such as when the loop is usually
terminated with break).
CPython implementation detail: xrange() is intended to be
simple and fast. Implementations may impose restrictions to
achieve this. The C implementation of Python restricts all
arguments to native C longs (“short” Python integers), and
also requires that the number of elements fit in a native C
long. If a larger range is needed, an alternate version can
be crafted using the itertools module: islice(count(start, step), (stop-start+step-1+2*(step<0))//step).
至于效率, 文档里也说了, 两者性能差不多, 除非 是个非常大的range,或则
在loop的时候用到break,这样的化,xrange性能更佳。 原因是:This is an opaque sequence type
which yields the same values as the corresponding list, without
actually storing them all simultaneously.
故事还没有结束, 那我们以后都应该用xrange吗?NO! 在3.X后的版本,两者的区别没了,因为:
In python 3,
range replaces xrange so that
If you want a
python 3 behavior in python 2, you can write
所以,如非必要,否则不要 大量使用xrange,
否则以后在3.X版本(肯定是要取代2.x版本)种,你就需要修改你的习惯了和你曾经编写过的代码了,习惯一旦养成,改往往是非常痛苦的!
xrange(3): // 返回 xrange类型
range(3) : // 返回 list类型
之间的区别,官方doc:
xrange([start], stop[, step])
This function is very similar to range(), but returns an “xrange object” instead of a
list. This is an opaque sequence type which yields the same
values as the corresponding list, without actually storing
them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create
the values when asked for them) except when a very large range
is used on a memory-starved machine or when all of the range’s
elements are never used (such as when the loop is usually
terminated with break).
CPython implementation detail: xrange() is intended to be
simple and fast. Implementations may impose restrictions to
achieve this. The C implementation of Python restricts all
arguments to native C longs (“short” Python integers), and
also requires that the number of elements fit in a native C
long. If a larger range is needed, an alternate version can
be crafted using the itertools module: islice(count(start, step), (stop-start+step-1+2*(step<0))//step).
至于效率, 文档里也说了, 两者性能差不多, 除非 是个非常大的range,或则
在loop的时候用到break,这样的化,xrange性能更佳。 原因是:This is an opaque sequence type
which yields the same values as the corresponding list, without
actually storing them all simultaneously.
故事还没有结束, 那我们以后都应该用xrange吗?NO! 在3.X后的版本,两者的区别没了,因为:
In python 3,
range replaces xrange so that
python 2| python 3
------------------------
xrange(10)| range(10)
range(10)| list(range(10))
If you want a
python 3 behavior in python 2, you can write
import sys
if sys.version_info <(3,):
range = xrange
所以,如非必要,否则不要 大量使用xrange,
否则以后在3.X版本(肯定是要取代2.x版本)种,你就需要修改你的习惯了和你曾经编写过的代码了,习惯一旦养成,改往往是非常痛苦的!
相关文章推荐
- python之range(), xrange()
- python中range和xrange的用法与区别
- python中range和xrange的区别
- python中的range和xrange的使用和区别
- demo_2. Python集合之间的关系运算
- Python的range和xrange
- python中xrange和range的异同
- ZH奶酪:Python中range和xrange的区别
- python中xrange和range的异同
- Python中类、实例、方法之间的关系
- Amusing Python 2: range/xrange
- python中xrange和range的异同
- python np.arange,np.linspace和np.logspace之间的区别
- python中range和xrange的区别
- Apollo,Python,Delphi与Oracle之间的神话关系
- Python比较for循环、列表解析、迭代器、map之间运算时间关系
- Python中Range和XRange的区别
- Python包管理工具之间的关系
- python range 与xrange的区别
- python 基础2 编码转换 pycharm 配置 运算符 基本数据类型int str list tupple dict for循环 enumerate序列方法 range和xrange