Python list-generated, list-derived

Source: Internet
Author: User
Tags python list

By using the List Builder, you can quickly generate a list that can derive another list from a list, while the code is very concise.

format [x for x in content][x for x in content if condition]

1: To put the generated element x in front, execution, the first to execute the later for loop

2: You can have multiple for loops followed by a For loop, or you can add an if condition after the for loop

The 3:for loop can be followed by any iterator (tuple, list, generator) in any way. ) As long as there is a value in the object that can iterate.

1: Single-loop list generation

String Generation List

print ([x for X in ' ABCD ']) #[' A ', ' B ', ' C ', ' d ']

List Generation List

print ([x for X in range (10)]) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

List Build list if condition

print ([x for x in range] if x > 5]) #[6, 7, 8, 9]
2: Double-loop list generation
Print ([M + N for m in ' ABC ' for n ' XYZ ') "#[' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']

See document: List Generation--Teacher Liao's Blog

Python list-generated, list-derived

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.