And the range of the difference between Python xrange

Range (1000) returns an array of length 1000,

And xrange (1000) returns a generator returns a number in case of need, in which case occupancy of the space will be greatly reduced.

So in order to improve performance, doing the cycle time should be used as much as possible xrange, unless you need to return an array with range.

In [9]: xrange(5)
Out[9]: xrange(5)

In [10]: range(5)
Out[10]: [0, 1, 2, 3, 4]

In [11]: list(xrange(5))
Out[11]: [0, 1, 2, 3, 4]

In [12]: print type(xrange(4))
<type 'xrange'>

In [13]: print type(range(4))
<type 'list'>

When using Python3, often found previously used xrange no, python3 the range is xrange.

Python2 in

Type >>> (range (10))
<type 'List'>
can be found, Python2 the return value in the range list, which means that the memory will be distributed to the respective spatial length list.

Python3 in

Type >>> (range (10))
<class 'range'>
Python3 is returned in a target range, and the data is not fully instantiated, so the memory space is only one object.

Python3 there are a number of other similar changes, is optimized for Python2, the specific use need to pay attention!
--------------------- 
Author: smile ancient and modern 
source: CSDN 
Original: https: //blog.csdn.net/u012735708/article/details/81910029 
copyright notice : This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/bl128ve900/article/details/94406221