Python list parsing and builder expressions

Source: Internet
Author: User
Tags iterable python list

List Parsing
You can use list parsing when you need to change the list instead of needing to create a new list. The list resolution expressions are:
[Expr for Iter_var in iterable] [Expr for Iter_var in iterable if COND_EXPR]
The first syntax: Iterate all the contents of iterable, each iteration, put the corresponding content in the iterable into Iter_var, then apply the Iter_var content in the expression, and then generate a list with the calculated value of the expression.
The second syntax: added the judgment statement, only satisfies the condition the content to put in the iterable the corresponding content in the Iter_var, then applies in the expression the Iter_var the content, finally uses the expression computation value to generate a list.
Examples are as follows:

The code is 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 you should consider using a generator expression instead of list parsing when the sequence is too long and you need to get only one element at a time. The syntax of a generator expression is the same as a list resolution, except that the generator expression is surrounded by (), not [], as follows:

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

Cases:

The code is as follows:

>>> l=  (i + 1 for i in  Range (Ten)  if i % 2)  >>> L <generator object < Genexpr> at 0xb749a52c> >>> l1=[] >>> for i in  l: ... l1.append (i)  ... >>> L1 [2, 4, 6, 8,  ] 

The generator expression does not really create a list of numbers, but instead returns a generator that "yields" the entry after each calculation of an entry. The generator expression uses lazy calculation (lazy evaluation, also translated as "deferred evaluation", which I thought would be more inert if called by need on demand) and was assigned only when retrieved (evaluated). Therefore, it is more efficient to use memory in the case of long lists. A generator object in Python was something like a lazy list. The elements is only evaluated as soon as a iterate over them.


Some notes:
1. Use loops rather than list parsing when you need to just perform a loop, which is more consistent with the intuitive nature of Python's advocacy.

The code is 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 way.
For example, when copying a list, use: L1=list (L), you do not need to use:

The code is as follows:

L1=[x for x in L]

3. If you need to call and return results for each element, use L1=map (f,l) instead of l1=[f (x) for x in L]


Python list parsing and builder expressions

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.