range、xrange 和 randrange 的用法和区别

作者: rebirth_2017 | 来源:发表于2017-05-23 15:21 被阅读89次

    概括起来,我的理解:


    range、xrange 和 randrange 的用法和区别

    下面是具体介绍:

    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)
    >>> x
    xrange(2, 5)
    >>> list(x)
    [2, 3, 4]
    >>> x[0]
    2
    

    与range的区别:

    for i in range(0, 100):
     print i
    
    for 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 a
    3
    >>> a=randrange(2,5)
    >>> print a
    4
    >>>
    

    相关文章

      网友评论

        本文标题:range、xrange 和 randrange 的用法和区别

        本文链接:https://www.haomeiwen.com/subject/czzwxxtx.html