Python list parsing

Source: Internet
Author: User

List parsing, primarily for dynamically creating lists

This article focuses on the use of the combination of lambda, map (), and filter () with the list parsing statements

The basic syntax for list parsing is: [Expr for Iter_var in iterable]

At the heart of this statement is the for loop, which iterates over all the entries of the Iterable object. The preceding expr applies to each member of the sequence, and the final result value is the list that the expression produces.

1. Basic use

Let's take an example.

To test in idle:

>>> [I for I in Range (0,8)]

[0,1,2,3,4,5,6,7]

Where I is the basic syntax of expr is also iter_val; in another way, let's take a look at the values inside and multiply all the members by 2.

>>> [i*2 for I in Range (0,8)]

[0, 2, 4, 6, 8, 10, 12, 14]

This is a list resolution that is written exactly in the basic syntax

2. Add a judging condition after the recycle statement

Extended version syntax: [Expr for Iter_val in iterable if COND_EXPR]

We can also expand and add a few more statements to filter the list, as long as we can divide the number by 2

>>> [I for I in range (1,8) if i%2 = = 0]
[2, 4, 6]

This statement is similar to using filter, so we can also use the Python built-in filter function to achieve the same value

>>> L = filter (lambda x:x%2==0, Range (1,8))
>>> for I in L:
I


2
4
6

But found no, there are some different, because I did not print directly out the list. Why is it? Because the return value of the filter is a generator (generator), the generator is

You cannot know all the values, you can only get the next value in an iterative way

3. Map to achieve the same results as list resolution

>>> map (Lambda x:x*2, Range (1,8))

[2, 4, 6, 8, 10, 12, 14]

Use it to get the same effect as [x * 2 for X in range (1,8)], but use the latter to be more efficient than map ()

4. Generating matrices

When you need to get a matrix of 3 rows and 5 columns, it's simple:

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

(1, 0), (1, 1), (1, 2), (1, 3), (1, 4),

(2, 0), (2, 1), (2, 2), (2, 3), (2, 4)]

You can also find more reference list parsing data in Pep 202

Python list parsing

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.