List parsing and generating expressions in Python _python

Source: Internet
Author: User
Tags generator iterable in python
List Resolution

You can use list resolution when you need to change the list instead of creating a new one. The list resolution expression is:

[Expr for Iter_var in iterable] [Expr for Iter_var in iterable if COND_EXPR]
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.

examples are as follows:
Copy Code code as follows:

>>> l= [(x+1,y+1) for x in range (3) for Y in range (5)]
>>> L
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3-5)]
>>> n=[x+10 for x in range (ten) if X>5]
>>> N
[16, 17, 18, 19]

Builder Expression

The builder expression is introduced in python2.4, and when the sequence is too long, you should consider using a builder expression instead of a list resolution each time you need to get only one element. The syntax of the builder expression is the same as for list parsing, except that the builder expression is () enclosed instead of [], as follows:
Copy Code code as follows:

(Expr for Iter_var in iterable)
(Expr for Iter_var in iterable if cond_expr)

Cases:
Copy Code code as follows:

>>> l= (i + 1 for I in range (a) if I% 2)
>>> L
<generator Object <genexpr> at 0xb749a52c>
>>> l1=[]
>>> for I in L:
... L1.append (i)
...
>>> L1
[2, 4, 6, 8, 10]

Instead of actually creating a list of numbers, the builder expression returns a generator that, after each calculation of an entry, "generates" (yield). The builder expression uses the "lazy calculation" (lazy evaluation, also translated as "deferred evaluation", which I think translates to inertia better by invoking call by need on demand) and is only assigned when retrieved (evaluated), So it's more efficient to use memory when the list is longer. A generator object in Python are something like a lazy list. The elements are only evaluated as soon as your iterate over them.

Some notes:

1. It is more consistent with the intuitive Python advocates that you use loops rather than list parsing when you need to just perform a loop.
Copy Code code as follows:

For item in sequence:
Process (item)

2. Do not use list parsing when there are built-in operations or types that can be implemented in a more straightforward manner.

For example, when copying a list, use: L1=list (L) without using:
Copy Code code as follows:

L1=[x for x in L]

3. If you need to call each element and return the result, use L1=map (f,l) instead of l1=[f (x) for x in L]
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.