You may be confused when you first see the list comprehension. They are a neat way to create and use lists. Understanding list comprehensions is useful, because you might see list comprehensions in other people's code. Let's understand the following list comprehension.
Derivation of Number List
Recalling what we have learned before, we can create a list containing the first 10 numbers as follows:
squares =[]for number inrange(1,11):
new_square = number**2
squares.append(new_square)for square in squares:print(square)
In the above code, we have implemented the function of creating a list of 10 numbers, squaring each number and storing them in a new array. The code is a bit verbose, we can omit the new_square parameter in the for loop to simplify the code. Using list comprehensions can further simplify the code, as shown below:
squares =[number**2for number inrange(1,11)]for square in squares:print(square)
The squaring operation and the process of generating a new list are condensed into one line of code. Are you dizzy, let's take a look at what happened to this line of code.
First we define a list named squares.
Next, look at the code in parentheses in the list:
for number inrange(1,11)
It creates a loop between 1-10 and stores each number in the variable number. Next, let's take a look at what operations are done on the number in each loop.
number**2
Each number is squared and the result is stored in a defined queue. We can read this line of code in the following language:
squares =[raise number to the second power,for each number in the range 1-10]
Other examples
The previous example is a square operation on a number. The following code is a multiplication operation on a number. Read the code carefully to experience the usage of a number list expression.
# Make an empty list that will hold the even numbers.
evens =[]
# Loop through the numbers 1-10, double each one, and add it to our list.for number inrange(1,11):
evens.append(number*2)
# Show that our list is correct:for even in evens:print(even)
Comprehension of non-number lists
We can also use deductions in non-numeric lists. In the following example, we will create a non-number list, and then use the deduction to generate a new list. The source code without deduction is as follows:
# Consider some students.
students =['bernice','aaron','cody']
# Let's turn them into great students.
great_students =[]for student in students:
great_students.append(student.title()+" the great!")
# Let's greet each great student.for great_student in great_students:print("Hello, "+ great_student)
We want to write the derivation shown below:
great_students = [add ‘the great’ to each student, for each student in the list of students]
The code is as follows:
# Consider some students.
students =['bernice','aaron','cody']
# Let's turn them into great students.
great_students =[student.title()+" the great!"for student in students]
# Let's greet each great student.for great_student in great_students:print("Hello, "+ great_student)
Extended exercise:
Generate a list of [[1,2,3],[4,5,6]....] The maximum value is within 100
First consider how we should write under normal circumstances
a =[]for x inrange(1,100,3):
a.append([x,x+1,x+2])
And then convert it into a list comprehension
a =[[x,x+1,x+2]for x inrange(1,100,3)]
The above is the detailed content of what python list comprehension is. For more information about the meaning and usage of python list comprehension, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts