python--List-generated--8

Source: Internet
Author: User

Original blog, reprint please indicate the source--Zhou Xuewei http://www.cnblogs.com/zxouxuewei/

I. Build list

To generate list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], we can use range (1, 11):

>>> Range (1) [123456  789]

But what if you want to generate [1x1, 2x2, 3x3, ..., 10x10]? Method One is the loop:

>>> L = [] for in range (1):     .... *>>> l[149  the  - Bayi []

But the loop is too cumbersome, and the list generation can use a line of statements instead of loops to generate the list above:

 for  in range (1) [14936  []

This is a Python-specific list generation. With list generation, you can generate lists in very concise code.

When writing list generation, put the element x * x to the front, followed by the For loop, you can create a list, very useful, write a few more times, you will soon be familiar with this syntax.

Two. Complex expressions

Iterations that use a for loop can not only iterate through the normal list, but also iterate over the Dict.

Assume the following dict:

' Adam '  the ' Lisa '  - ' Bart '  i}

It can be turned into an HTML table by a complex list-building:

TDS = ['<tr><td>%s</td><td>%s</td></tr>'% (name, score) forName, scoreinchd.iteritems ()]print'<table>'Print'<tr><th>Name</th><th>Score</th><tr>'Print'\ n'. Join (TDS) Print'</table>'

Note: The string can be formatted by%, substituting%s with the specified parameter . The join () method of a string can be used to stitch a list into a string.

Save the printed result as an HTML file, and you'll see the effect in the browser:

<table border="1"><tr><th>name</th><th>score</th ><tr><tr><td>Lisa</td><td></TD></TR><TR><TD >Adam</td><td></td></tr><tr><td>Bart</td><td> </td></tr></table>

Three. Conditional filtering

A list-generated for loop can also be followed by an if judgment . For example:

 for  in range (1) [14936  []

If we want even squares, without changing the range (), you can filter by if:

 for  in range (1if20] [436  [+]

With the IF condition, the current element of the loop is added to the list only if the if is judged to be True.

Four. Multi-layer expressions

For loops can be nested, so in a list build, you can also use a multi-layer for loop to generate the list.

For the string ' ABC ' and ' 123 ', a two-layer loop can be used to generate a full array:

>>> [M + N forMinch 'ABC'  forNinch '123']['A1','A2','A3','B1','B2','B3','C1','C2','C3']

The Loop code is translated as follows:

L = []for in'ABC':    for in ' 123 ' :         + N)

python--List-generated--8

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.