Python generator expression and list Parsing

Source: Internet
Author: User
Recently, I was confused about list parsing and generator expressions when I was learning python. On the surface, the two are very similar. There is only one difference: List parsing uses brackets [] for inclusion, and generator expressions use parentheses () for inclusion. In this article, we will take a closer look at python generator expressions and list parsing. In most cases, traversing a set is to apply an action to an element or filter it. If you have read the second part of this article, you should remember that the built-in functions map and filter provide these functions, but Python still provides language-level support for these operations.

(X + 1 for x in lst) # generator expression, which returns the iterator. The external brackets can be omitted when used as parameters. [X + 1 for x in lst] # list parsing, returns list

As you can see, generator expressions and list parsing (Note: there are many types of translation here, such as list expansion and list derivation, which refer to the same meaning) have little difference, when people mention this feature, it is often described as list parsing only for the sake of simplicity. However, since the returned iterator does not calculate all elements at the beginning, this gives more flexibility and avoids many unnecessary computations, therefore, unless you explicitly want to return the list, you should always use the generator expression. In the following text, I will not distinguish these two forms :)

You can also provide the if clause for list parsing for filtering:

(x+1 for x in lst if x!=0)

Or multiple for clauses can be provided for nested loops. the nesting order is the order of for clauses:

((x, y) for x in range(3) for y in range(x))

List Parsing is a clear Pythonic. I often encounter two problems of using list resolution, which should belong to the best practice, but these two problems are very typical, so I would like to mention them here:

The first problem is that, because the action on the element application is too complex to be written using an expression, list resolution is not used. This is a typical example of no change in thinking. If we encapsulate an action into a function, isn't it an expression?

The second problem is that, because the conditions in the if clause need to be calculated, and the results also need to be calculated twice, as shown in the following figure:

(x.doSomething() for x in lst if x.doSomething()>0)

This write is really bad, but it can be solved by combining the list parsing:

(x for x in (y.doSomething() for y in lst) if x>0)

The internal list parsing variable can also use x, but for clarity, we changed to y. Or, you can write two expressions:

tmp = (x.doSomething() for x in lst)(x for x in tmp if x > 0)

List parsing can replace the vast majority of scenarios where map and filter are needed. For this reason, the famous static check tool pylint uses the columns of map and filter as warnings.

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.