There is a special expression in Python called a derivation . Its function is to take a data structure as input, then filter and calculate it, and finally output another data structure. According to different data structures, it can be divided into list comprehension, set comprehension and dictionary comprehension. Let’s first focus on the most commonly used list comprehensions.
Let’s first take a look at the syntax format of list comprehensions:
listname=[expressionforvariablein object (ifcondition)]
listname: the name of the newly generated list.
expression: expression.
variable: variable name.
(if condition): used to select a list that meets the requirements from the object.
We start from three aspects: a list of values within a specified range, a list of specified conditions, and a list composed of elements that meet the conditions.
Let's first think about when we need to generate 10 numbers and store them in a list. Let's first look at the common way:
listname=[]foriinrange(10):listname.append(i)print(listname)
The output is:
[0,1,2,3,4,5,6,7,8,9]
Using a list comprehension only requires one line:
listname=[iforiinrange(10)]
The output is:
[0,1,2,3,4,5,6,7,8,9]
This approach simplifies our operations when defining lists.
Suppose we know that a list is listname = [1,3,5,6,7,9,10,23,26,28,64,98]. We want to find it and add all the numbers in it by 5. The ordinary method:
listname=[1,3,5,6,7,9,10,23,26,28,64,98]foriinrange(len(listname)):listname[i]+=5print(listname)
The output is:
[6,8,10,11,12,14,15,28,31,33,69,103]
Using list comprehensions is also very concise:
listname=[1,3,5,6,7,9,10,23,26,28,64,98]listname=[i+5foriinlistname]
Output:
[6,8,10,11,12,14,15,28,31,33,69,103]
This type of list comprehension is more complex than the first two, but it can simplify more code.
Let’s take an example to look at the code format:
It is known that a list is listname = [8,33,53,64,73,95,101,123,126,164,198], then we need to find the numbers in the list that are less than and greater than 100, then multiply them by 0.8, and finally return them to the list.
If we use the normal method:
listname=[10,20,30,40,60,120,130,140,160,180,200]newlist=[]#Create a new list to store foriinrange(len(listname)):#Index value traversal iflistname[i]>100:#Find a number greater than 100 listname[ i]*=0.8#Multiply by 0.8 newlist.append(listname[i])#Add to new list print(newlist)
The output is:
[96.0,104.0,112.0,128.0,144.0,160.0]
Use list comprehensions:
listname=[10,20,30,40,60,120,130,140,160,180,200]newlist=[i*0.8foriinlistnameifi>100]print(newlist)
Output result:
[96.0,104.0,112.0,128.0,144.0,160.0]
Let’s analyze it based on the grammatical structure of this example:
We can use this method when using complex list comprehensions. It can be understood that we first select the elements that meet the conditions (conditional statements) from the object, then process the output expression, and finally store them in the list. , forming a new list.
Here are a few example questions you can try.
1. List [1,2,13,22,25], please use list comprehension to extract numbers greater than 10, square each number, and finally output.
The running result is:
[169,484,625]
The code is as follows, you can try to complete the reference answer first.
list=[1,2,13,22,25]newlist=[i*iforiinlistifi>10]print(newlist)
2. Use list comprehension to find all odd numbers in the list and construct a new list, list= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The code is as follows:
list=[1,2,3,4,5,6,7,8,9,10]newlist=[iforiinlistifi%2==1]print(newlist)
The output is:
[1,3,5,7,9]