range、xrange 和 randrange 的用法和区别
来源:互联网 发布:通信算法工程师 工作 编辑:程序博客网 时间:2024/06/03 23:39
概括起来,我的理解:
下面是具体介绍:
1. range([start], stop[, step])
返回等差数列。构建等差数列,起点是start,终点是stop,但不包含stop,公差是step。start 和 step 是可选项,没给出start时,从0开始;没给出step时,默认公差为1。
以下为帮助文档解释:
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 largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step 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)[]
2.xrange([start], stop[, step])
xrange与range类似,只是返回的是一个“xrange object”对象,而非数组list。要生成很大的数字序列的时候,用xrange会比range性能优很多,因为不需要一上来就开辟一块很大的内存空间,这两个基本上都是在循环的时候用。
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).
>>> x=xrange(2,5)>>> xxrange(2, 5)>>> list(x)[2, 3, 4]>>> x[0]2
与range的区别:
for i in range(0, 100): print ifor i in xrange(0, 100): print i
这两个输出的结果都是一样的,实际上有很多不同,range会直接生成一个list对象:
>>> a = range(0,100)>>> print a[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]>>> print type(a)<type 'list'>>>> print a[0]0>>>
而xrange将返回一个生成器对象:
>>> a = xrange(0,100)>>> print type(a)<type 'xrange'>>>> print a[0]0>>> list(a)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]>>>
3.random.randrange([start], stop[, step])
返回的是一个随机数,这个随机数来源于 range([start], stop[, step]) 。
Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.
>>> from random import randrange>>> a=randrange(2,5)>>> print a3>>> a=randrange(2,5)>>> print a4>>>
- range、xrange 和 randrange 的用法和区别
- Range和XRange的区别
- range() 和xrange()的区别
- range和xrange的区别
- xrange和range的区别
- range和xrange的区别
- range和xrange的区别
- xrange 和range区别
- xrange和range区别
- python中range和xrange的用法与区别
- Python中range和xrange的用法
- python range和xrange的用法
- Python中Range和XRange的区别
- Python中Range和XRange的区别
- Python中Range和XRange的区别
- Python中Range和XRange的区别
- python中range 和 xrange的区别
- Python中range和xrange的区别
- 天天写业务代码的程序员怎么成为大牛(转自知乎李运华的回答 PS:很干货)
- html嵌入动画
- Openjudge 螺旋加密
- html中插入javascript脚本画条直线
- 怎么把本地项目和远程git仓库相连通
- range、xrange 和 randrange 的用法和区别
- pip install whl 错误版本解决(暨在win7 python3下装 opencv)
- OCR和votedisk备份与还原
- html中插入javascript画矩形
- str2float
- JVM学习(初识JVM)
- 雷军说小米6下月起不再耍猴 你信吗?
- 作业批处理
- Python学习笔记(四)