Python list derivation

Source: Internet
Author: User

1 List-derived definitions

The list derivation can be very concise to construct a new list: The resulting element can be transformed with a simple expression

2 List derivation syntax

The basic format is as follows:

[Expr for value in collection if condition]

Filter conditions are optional, depending on the actual application, leaving only the expression

Example of a list derivation:

12 l=["egg%s"%forinrange(10)]print(l)

Similar to this for code:

1234 egg_list=[]forinrange(10):    egg_list.append("egg%s"%i)print(egg_list)

  

The list derivation can also add more if to judge the For loop

1234 l=[‘egg%s‘%forinrange(1,101)]l=[‘egg%s‘%forinrange(1,101ifi >50]l=[‘egg%s‘%forinrange(1,101ifi >50 ifi<60]print(l)

 

3 List Derivation Benefits

Convenient, change the programming habit, belong to the declarative programming

Example:

1234 l=[1,2,3,4]s="hello"l1=[(num,i) fornum inforins]print(l1)

The output is:

1 [(1‘h‘), (1‘e‘), (1‘l‘), (1‘l‘), (1‘o‘), (2‘h‘), (2‘e‘), (2‘l‘), (2‘l‘), <br>(2‘o‘), (3‘h‘), (3‘e‘), (3‘l‘), (3‘l‘), (3‘o‘), (4‘h‘), (4‘e‘), (4‘l‘), <br>(4‘l‘), (4‘o‘)]

  

This list derivation is equivalent to:

12345678 l=[1,2,3,4]s="hello"l1=[]fornum inl:    forins:        t=(num,i)        l1.append(t)print(l1)

4 Examples of list expressions

1234567891011 importosg=os.walk("C:\python_fullstack_wen\day24\wen")file_path_list=[]foring:    forini[-1]:        file_path_list.append("%s\\%s"%(i[0],j))print(file_path_list)g=os.walk("C:\python_fullstack_wen\day24\wen")file_path_list=["%s\\%s"%(i[0],j) forinforini[-1]]print(file_path_list)

Output Result:

12 [‘C:\\python_fullstack_wen\\day24\\wen\\yan1.txt‘‘C:\\python_fullstack_wen\\day24\\wen\\wen1<br>\\yan1.txt‘‘C:\\python_fullstack_wen\\day24\\wen\\wen1\\jie1\\yan1.txt‘, <br>‘C:\\python_fullstack_wen\\day24\\wen\\wen1\\yan1\\yan1.txt‘, <br>‘C:\\python_fullstack_wen\\day24\\wen\\wen2\\yan2.txt‘][‘C:\\python_fullstack_wen\\day24\\wen\\yan1.txt‘‘C:\\python_fullstack_wen\\day24\\wen\\wen1\\yan1.txt‘‘C:\\python_fullstack_wen\\day24\\wen\\wen1\\jie1\\yan1.txt‘‘C:\\python_fullstack_wen\\day24\\wen\\wen1\\yan1\\yan1.txt‘‘C:\\python_fullstack_wen\\day24\\wen\\wen2\\yan2.txt‘]

Python list derivation

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.