引自 python官方文档 http://docs.python.org/2/library/functions.html

  range(stop)  range(startstop[, step])

This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largeststart + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stopstep must not be zero (or else ValueError is raised). Example:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5)
[0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3)
[0, 3, 6, 9]
>>> range(0, -10, -1)
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]

xrange(stop)xrange(startstop[, 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() overrange() 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 withbreak). For more information on xrange objects, see XRange Type and Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange.

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 theitertools module: islice(count(start, step), (stop-start+step-1+2*(step<0))//step).

官方文档中说明range返回的是一个list,而xrange返回的是一个xrange对象,并且xrange更加简洁快速,所以在程序当中应当尽量使用xrange。

转载于:https://www.cnblogs.com/jeesezhang/p/3542865.html

python range 和 xrange 区别相关推荐

  1. Python range和xrange的区别和联系

    Python range和xrange的区别和联系 一言以蔽之 range产生的是一个列表,而xrange产生的是一个类似迭代器的. 所以对于较大的集合时候,xrange比range性能好. 因为ra ...

  2. Python中的range和xrange区别

    range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列. range示例: >>> r ...

  3. 【Python】range和xrange区别

    转自:http://www.cnblogs.com/zhangjing0502/archive/2012/05/16/2503880.html range     函数说明:range([start, ...

  4. Python range 和 xrange的区别

    Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表. Python3 list() 函数是对象迭代器,可以把range()返回的可迭 ...

  5. python range 步长为负数_【Python面试】 说说Python中xrange和range的区别?

    公众号新增加了一个栏目,就是每天给大家解答一道Python常见的面试题,反正每天不贪多,一天一题,正好合适,只希望这个面试栏目,给那些正在准备面试的同学,提供一点点帮助! 小猿会从最基础的面试题开始, ...

  6. python中range和xrange的区别_ZH奶酪:Python中range和xrange的区别

    直观说明:http://ciniao.me/article.php?id=17 原因分析:http://blog.csdn.net/liangliyin/article/details/5980505 ...

  7. python中range和xrange的区别_python中range和xrange的区别

    range()是Python的内置函数,用于创建整数的列表,可以生成递增或者递减的数列.xrange也有相同的功能, 今天来看下它们之间的不同. range 函数说明:range([start,] s ...

  8. [转载] Python中的xrange和range的区别

    参考链接: Python中的range()和xrange() 在python2 中 range(start,end,step)返回一个列表,返回的结果是可迭代对象,但不是迭代器.iter()转化为列表 ...

  9. python中range和arange的区别_Python——range()、xrange()和np.arange()应用说明

    (1)range()和xrange()函数在 python 2.x 版本中,同时存在range()和xrange()函数,其中,range()返回值是一个列表,xrange()返回值是一个迭代值: 在 ...

  10. Python中range与xrange的区别

    文档中对range与xrange的介绍如下: range([start], stop[,step]) This is a versatile function to create lists cont ...

最新文章

  1. Skynet入门范例之sproto
  2. java 网络爬虫 正则表达式_【干货】Java网络爬虫基础知识
  3. php表单显示mysql数据库_php用表单形式显示数据库信息
  4. C#LeetCode刷题-极小化极大
  5. c语言2 amp 3结果,C语言里23=什么?
  6. python基础语法测评_3. Python基础语法
  7. 距离考研还有2天,我还活着
  8. Python高性能计算库—Numba
  9. 【软件与系统安全】栈溢出利用的分析
  10. 2k21sports服务器暂时不可用,NBA2K20服务器不可用怎么解决 nba2k20进不去游戏解决办法...
  11. 《世界500强企业员工的50条生存法则》(Yanlz+Unity+SteamVR+5G+AI+VR云游戏+生存法则+潜规则+提升竞争力+术业有专攻+卓越理念+立钻哥哥+==)
  12. 国外有python专业的大学_有哪些国外大学非常容易申请?
  13. Ubuntu20.04安装Nvidia驱动——4060显卡(黑屏解决方法)
  14. 中国第21批援赞比亚军医组凯旋
  15. MYSQL中,CAST函数的使用规则
  16. 《结构动力分析的MATLAB实现》,结构动力分析的MATLAB实现
  17. Python爬虫的起点,一文轻松入门
  18. ESD9X5VU-2/TR瞬态电压抑制器WILLSEM ESD9X5VU 1线单向超低电容
  19. hao123静态html源码,115le仿hao123网址导航整站静态html v7
  20. 第三方谷歌市场替代软件Blackmart 可用下载Google Play 上的应用

热门文章

  1. JavaScript权威指南--chapter 8函数
  2. .NET 中的Cache
  3. 经验案例:当配置为共享/静态WEP加密时,客户端无法从DHCP获取IP地址
  4. python入门24 json模块
  5. UI 假死的可能性和处理方法总结
  6. java知识总结-13
  7. NFinal 控制器—URL
  8. 如何提升会员列表数据的质量
  9. 【排序算法】选择排序
  10. leetcode 153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array)