range 
函数说明:range([start,] stop[, step]),根据startstop指定的范围以及step设定的步长,生成一个列表。

xrange 
函数说明:和range 的用法完全相同,但是返回的是一个生成器。在python3中已经由range取代xrange

  1. print range(10) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  2. print xrange(10) #xrange(10)
  3.  
  4. print range(2,10) #[2, 3, 4, 5, 6, 7, 8, 9]
  5. print xrange(2,10) #xrange(2, 10)
  6.  
  7. print range(2,10,2) #[2, 4, 6, 8]
  8. print xrange(2,10,2) #xrange(2, 10, 2)
  9.  
  10. print type(range(1,10)) #<type 'list'>
  11. print type(xrange(1,10)) #<type 'xrange'>
  12. print type(list(xrange(1,10))) #<type 'list'>

但是要生成很大的数字序列的时候,用xrange会比range性能优很多,因为不需要一上来就开辟一块很大的内存空间,这两个基本上都是在循环的时候用。再循环中尽量使用 xrange 这样性能可以得到提高,除非你要返回一个列表。

  1. l=range(0,20)
  2. print l[0],l[5]#0 5
  3.  
  4. for i in range(0,5):print i #0 1 2 3 4
  5. for i in xrange(0,5):print i #0 1 2 3 4

关于randrange,用法和random一样,但返回是一个结果

  1. from random import randrange
  2. a=randrange(10,100,2)
  3. print a #

python中range、xrange和randrange的区别的更多相关文章

  1. python中import和from...import...的区别

    python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...

  2. 转发 python中file和open有什么区别

    python中file和open有什么区别?2008-04-15 11:30地痞小流氓 | 分类:python | 浏览3426次python中file和open有什么区别?都是打开文件,说的越详细越 ...

  3. python中range()函数的用法

    python中range()函数可创建一个整数列表,一般用在for循环中. range()函数语法: range(start,stop[,step]) 参数说明: star: 计数从star开始.默认 ...

  4. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  5. ZH奶酪:Python中range和xrange的区别

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

  6. python之旅:python中range()和len()函数区别

    函数:len() 作用:返回字符串.列表.字典.元组等长度 语法:len(str) 参数: str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度 实例 1.计算字符 ...

  7. python中range()和len()函数区别

    函数:len() 作用:返回字符串.列表.字典.元组等长度 语法:len(str) 参数: str:要计算的字符串.列表.字典.元组等 返回值:字符串.列表.字典.元组等元素的长度 实例 1.计算字符 ...

  8. python 中range numpy.arange 和 numpy.linspace 的区别

    1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...

  9. Python中list,tuple,dict,set的区别和用法

    Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个内置数据类型功不可没,他们即是list, tuple, dict, set.这里对他们进行一个简明的总结. List ...

  10. Python中list,tuple,dict,set的区别和用法(转)

    原文地址:http://www.cnblogs.com/soaringEveryday/p/5044007.html Python语言简洁明了,可以用较少的代码实现同样的功能.这其中Python的四个 ...

随机推荐

  1. cgo -rpath指定动态库路径

    // #cgo CFLAGS: -Wall // #cgo LDFLAGS: -Wl,-rpath="/home/liuliang/ffmpeg-build/lib" // #cg ...

  2. 【转】关于编译链接——gcc/g++

    添加运行时共享库目录 运行使用共享库的程序需要加载共享库(不同于G++ 编译时指定的链接库),添加共享库的步骤: 修改文件 /etc/ld.so.conf 添加共享库目录 运行 ldconfig 同步 ...

  3. python面向对象-3类的静态方法和类方法

    还是以上次的洗衣机例子: class Washer: company='ZBL' def __init__(self,water=10,scour=2): self._water=water #不想让 ...

  4. Codeforces777E. Hanoi Factory 2017-05-04 18:10 42人阅读 评论(0) 收藏

    E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  5. Codeforces735B Urbanization 2016-12-13 11:58 114人阅读 评论(0) 收藏

    B. Urbanization time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. 使用WinSCP在Windows和Linux系统之间传输文件

    小梅哥编写,未经许可,严禁用于任何商业用途 2018年6月30日 在日常SoC开发中,我们经常需要在Windows和Linux系统之间传输文件,例如在Windows系统上的DS-5集成开发环境中编写好 ...

  7. SPOJ10606 BALNUM - Balanced Numbers(数位DP+状压)

    Balanced numbers have been used by mathematicians for centuries. A positive integer is considered a ...

  8. java socket编程(也是学习多线程的例子)详细版----转

    7.2 面向套接字编程    我们已经通过了解Socket的接口,知其所以然,下面我们就将通过具体的案例,来熟悉Socket的具体工作方式 7.2.1使用套接字实现基于TCP协议的服务器和客户机程序  ...

  9. Oracle EBS标准错误信息如何追踪 (Debug)

    http://www.cnblogs.com/songdavid/articles/2067534.html 调用EBS标准API的时候,可能会返回一些让人看不懂的错误,比如最近我在开发rcv_tra ...

  10. pl/sql 语言设置

    1.select * from v$nls_parameters 查询nls的参数,获得数据库服务器端的字符编码 NLS_LANGUAGE NLS_CHARACTERSET 2.修改本地环境变量 NL ...