Comparison and summary of Xrange () and range ()

Source: Internet
Author: User

One, two functions of the document:

1,xrange ():

xrange ( stop )
xrange ( start, stop[, step] )

This function was very similar to range () , but returns an Xrange object instead of a list. This is a 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 have to create the value s when asked for them) except when a very large range are used on a memory-starved machine or if all of the range ' s Eleme NTS was never used (such as when the loop was usually terminated with break ). For more information on Xrange objects, see xrange Type and Sequence types-str, Unicode, list, tuple, Bytea Rray, buffer, xrange .

CPython Implementation Detail: xrange () is intended to being simple and fast. Implementations impose restrictions to achieve. The c implementation of Python restricts all arguments to native C longs (' short ' python integers), and also requires that The number of elements fit in a native C long. If a larger range is needed, an alternate version can be crafted using the itertools module: islice (COUNT (STA RT, Step), (stop-start+step-1+2* (step<0))//step).

—————————————————————————————————————————————

xrange ( stop )
xrange ( start, stop[, step] )

This function is similar to the range () function, but returns a Xrange object instead of a list.

Xrange is an opaque array that produces the same values as the corresponding list, but xrange does not store the values. Xrange () has no obvious advantage over range () except for certain situations such as an array that is too large and out of memory, and where frequent termination causes all elements in the array to be inaccessible. This is because when the elements in the array are accessed, xrange () still needs to create them.

For more information about XRange (), see XRange Type and Sequence types-str, Unicode, list, tuple, ByteArray, buffer, XRange.

CPython Implementation Details: in order to make xrange () simple and fast, the compiler will use some strict restrictions to achieve this purpose. CPython restricts all parameters to long and requires that the number of elements not exceed the range of long. If you need a larger array, you can use a more refined model that uses the Circulator (Itertools):

islice (Start, Step), (stop-start+step-1+2* (step<0))//step).

2,range ():

Range ( stop )
Range ( start, stop[, step] )

This is a versatile function to create lists containing arithmetic progressions. It is the 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 was 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

being zero (or else valueerror is raised).

Example:

>>>

>>> range [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range (1, one) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>>> R Ange (0, 5) [0, 5, ten,, 25]>>> range (0,, 3) [0, 3, 6, 9]>>> range (0,-10,-1) [0,-1,-2,-3, -4, -5,-6,-7,-8, -9]>>> range (0) []>>> range (1, 0) []

—————————————————————————————————————————————

Range ( stop )
Range ( start, stop[, step] )

This multifunctional function can create a list of arithmetic sums, so it is often used in loops. The parameter of this function must be an integer. The default value for the step parameter is the default value of the 1;start parameter is 0. The function returns an integer list [start, start + step, start + 2 * step, ...]. If the stop parameter is positive, the last element in the list is close but not greater than it, or if the stop parameter is negative, the last element in the list is close but not less than it. The stop parameter cannot be 0 (if 0, an error will be added: ValueError).

Two, usage and summary

(not to be continued)

Comparison and summary of Xrange () and range ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.