0 Fundamentals python-19.8 Generator expression: When an iterator encounters a list resolution

Source: Internet
Author: User

Let's talk about generator expressions in this section.

Syntactically speaking, the generator expression is the same as the list parsing, except that the list parsing is placed inside the brackets, and the generator expression is enclosed in parentheses.


As you can see from the code above, the parentheses return a generator to use the list function to parse the contents.

To put it bluntly, from the results above, in fact, list parsing is using the list function to force the generator to generate all the results at once.

>>> [x for X in range (5)][0, 1, 2, 3, 4]



From the execution above, list parsing builds all the results in memory at once, and the generator expression simply returns an object, and this object supports iterative protocols, as long as it can be manipulated in an iterative environment.


For example, we can get the value of the object inside the generator by using for.



As in the above code, you can support the operation of an object in an environment that supports iterative protocols such as map, filter, and sorted.

Summary: This chapter focuses on the relationship between the generator and list resolution, and the generator supports iterative protocols that can manipulate objects in an iterative environment.

This chapter is here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Fundamentals python-19.8 Generator expression: When an iterator encounters a list resolution

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.