Detailed builder expressions in Python (generator expression)

Source: Internet
Author: User

  

Introduced

1, generator expressions (generator expression) are also called generator derivation or generator parsing, and the usage is very similar to the list derivation, in the form generator deduction using parentheses (parentheses) as the delimiter, Instead of the square brackets used by the list deduction (square brackets).

2, the biggest difference from the list derivation is that the result of the generator derivation is a generator object. The Builder object is similar to an iterator object and has the characteristic of lazy evaluation, which only generates new elements when needed, is more efficient than the list derivation, and occupies very little space, especially for large data processing situations.

3. When using the elements of a generator object, you can convert it to a list or tuple as needed, or you can use the next () method of the Generator object or the built-in function next () to traverse it, or use the For loop to traverse the element directly. But regardless of the method that accesses its elements,

Each element can only be accessed from the forward to the back, and cannot be accessed again

4, the elements that have been accessed, and those that are accessed using subscripts are not supported. When all of the elements have been accessed, if you need to revisit the elements, you must recreate the generator object, and the enumerate, filter, map, zip, and other iterator objects have the same characteristics.

  

  

#1. Creating Builder Objectsg = ((i+2) **2 forIinchRange (10))Printg#<generator Object <genexpr> at 0x0000000003517798>#2. Converting a generator object to a tupleA =tuple (g)Printa#(4, 9, +, -------------------Bayi, 121)#3, the Generator object has traversed the end, no elementsPrintList (g)#[]#4. Recreate the Builder objectg = ((i+2) **2 forIinchRange (10))#5. Get the element using the next () method of the Generator objectPrintG.next ()#4PrintG.next ()#9#6. Use the function next () to get the elements in the builder objectPrintNext (g)# -g= ((i+2) **2 forIinchRange (10))#7. Use loops to traverse elements in the builder object directly forIteminchg:PrintItem#8, the filter object also has similar characteristicsx = Filter (None, range (20))Printx#[1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one, A, A, (+), (+), +--)#9, the Map object also has similar characteristicsx = Map (str, RANGE (20))Printx#[' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' Ten ', ' one ', ' I ', ' + ', ' + ', ' + ', ' + ', ' + '

Detailed builder expressions in Python (generator expression)

Related Article

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.