List of Python parsing

Source: Internet
Author: User
Tags iterable

List parsing: the creation of a new list based on the original list


List-Resolved build lists, which can also be implemented with a for loop, have the advantage that the list expression is faster than the For loop.

List Resolution Expressions:


Expression One

[Expression for Iter_var in iterable]

Assigns the value in the Iteration object (iterable) to Iter_var and executes the expression


Cases:

>>> a = [1,2,3,4,5]

>>> B = [i**2 for i in a]

>>> b

[1, 4, 9, 16, 25]


Assigns the value in a to I,i execution i**2, the resulting value generates B


Expression two:

[Expression for Iter_var in iterable if condition]

Removes the value from the Iteration object (iterable), assigns to Iter_var if Contion is satisfied, and executes expression


Cases:

>>> a = [1,2,3,4,5]

>>>c = [i**2 for i in a if I% 2 = = 0 and I >=3]

>>> C

[16]

When the value in a is taken out and the value is even and greater than 3, the i**2 is executed, generating C

List of Python 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.