Python 3 list derivation of a little understanding!

Source: Internet
Author: User

Python 3 list derivation of a little understanding!

The list derivation of Python is generally difficult for beginners to understand, simply look at an example:

[x * x for x in range (1,100)]

The above is a very simple list deduction, I understand the way from right to left to see.

At 1.1, let's start with a range (1,100) to generate an iterator object (a list of 1-99), then iterate over each object of the list, then have each object perform a square operation and finally generate a list.

-----------------------------Split Line----------------------------------------------------------------------------------------- ------------------------------------------------------------

This may seem a bit more intuitive.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We can also use this:

[' The%s '% x for x in range (1,10)]

Here we use the formatted string%s, which is the same as it is understood. Right-to-left understanding: First generate a list with range (), then use for in to iterate over each object, and then put each object into the placeholder, and finally generate a list.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Split Line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[(x, Y) for x in range (1,100) for Y in range (1,100)]

Using the derivation list to generate a tuple, understand: First generate a list with range (), and then iterate, this operation performed two times, X at a time, y at a time, and then a list is generated using the tuple definition.

Python 3 list derivation of a little understanding!

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.