Go Interesting list parsing and builder expression __python in Python

Source: Internet
Author: User
Tags iterable
Python is a lovely and interesting dynamic language, powerful and efficient, the syntax is beautiful and simple, let's play the lovely side of it: List resolution and generator expressions.

List resolution:
Syntax: [Expr for Iter_var in iterable] or [expr for Iter_var in iterable if COND_EXPR]

Description
The first syntax: first iterate over all the content in iterable, each iteration, put the corresponding content in the iterable in the Iter_var, and then apply the Iter_var content in the expression, and finally generate a list with the calculated value of the expression.
The second kind of syntax: added the judgment statement, only satisfies the condition the content to put the corresponding content in the iterable in the Iter_var, then applies the Iter_var content in the expression, finally uses the expression the computation value to produce a list.

Example:
>>> [i + 1 for I in range (10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

>>> [i + 1 for I in range (ten) if I% 2]
[2, 4, 6, 8, 10]

How, very convenient, very natural.

Builder expression:
Syntax: (expr for Iter_var in iterable) or (expr for iter_var in iterable if cond_expr)

Description: List parsing is introduced in the earlier Python version (like the 2.0 version), the generator expression is the new content introduced in 2.4, which is similar to the syntax of list parsing, but the advantage of the generator expression is reflected in the large amount of data processing, because it uses better memory, More efficient, it doesn't create a list, just returns a generator. Of course, list parsing is not abandoned.

Example:
>>> (i + 1 for I in range (a) if I% 2)
<generator Object <genexpr> at 0x011dc5d0>

>>> g = (i + 1 for I in range (a) if I% 2)
>>> L = []
>>> for J in G:
L.append (j)
>>> L
[2, 4, 6, 8, 10]
As you can see from the above, list parsing and builder expression syntax are very similar, but in essence they are quite different.
Python is powerful, beautiful, efficient, concise and interesting, and hopefully more people will come to know it.
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.