A New Method for parsing the python list

Source: Internet
Author: User
Tags python list

Python list ParsingI feel like python is very flexible. When I first came into contact with it, especially when I learned other languages,
You may feel unaccustomed. How can you see what is wrong? What is strange? This is the magic of list resolution.

 

Python list Parsing is one of the python iterations, which is much faster than the python for loop. Next let's take a look at Python list parsing.
How it works.
Example:
A = [x + 2 for X in range (10)]
Print
Output result:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

The execution process of the above example is as follows:
Python iterates range (10) In the interpreter, extracts the content in the list in sequence, assigns the value to the leftmost X, and then executes the X + 2 operation,
And save the execution result in the list. After range (10) iteration, a new list is generated. The result is [2, 3, 4, 5, 6, 7, 8, 9, 10, 11].
It can be seen from the above that this is also a method for creating a python list.

If the above example is implemented using a python for loop, what would it be like?
A = []
For I in range (10 ):
A. append (I + 2)

Print

Output result:
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

As can be seen from the above, the python list Parsing is better than the python for loop parsing listCodeLess data, faster resolution, cool writing, and independent writing
The script is very simple, but it also has some shortcomings, and its readability is not very good, especially in the case of team development, it should be less useful, so the versatility is better, it is also good for everyone.

If you are interested in the python list or Python list operation method, you can check it out!

Author: Lao Wang @ Python tutorial
Old Wang Python provides Python tutorials and Python downloads related to pythn.

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.