Concatenating two range function results

前端 未结 8 1312
臣服心动
臣服心动 2020-11-27 17:04

Does range function allows concatenation ? Like i want to make a range(30) & concatenate it with range(2000, 5002). So my concatenated range w

8条回答
  •  情话喂你
    2020-11-27 17:28

    range() in Python 2.x returns a list:

    >>> range(10)
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    xrange() in Python 2.x returns an iterator:

    >>> xrange(10)
    xrange(10)
    

    And in Python 3 range() also returns an iterator:

    >>> r = range(10)
    >>> iterator = r.__iter__()
    >>> iterator.__next__()
    0
    >>> iterator.__next__()
    1
    >>> iterator.__next__()
    2
    

    So it is clear that you can not concatenate iterators other by using chain() as the other guy pointed out.

提交回复
热议问题