Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of python list derivation and how to use it

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the principle of python list derivation and how to use it". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "what is the principle of python list derivation and how to use it" can help you solve the problem.

Occasionally I see a line of code like this in python:

Data = [xylene 2 for x in range (0,5)] # now data = [0,1,4,9,16]

If you don't know the syntax rules behind it, it's hard to understand why you can write a "for" loop in square brackets in a list. In fact, this syntax is called list deduction, which is similar to lambda expressions and is used when the code is very short.

This article will explain the list derivation from two aspects:

(1) what is list derivation?

(2) how to use list derivation

1. What is list derivation?

List derivation corresponds to list comprehension in English and is sometimes translated into list parsing, which is a concise syntax for creating lists. Before we start to analyze it,

Let's take a look at the following more common ways to create lists:

Data = [] # create an empty list for x in range (- 5,5): if x > =-2: # if x > =-2, add I squared data.append (Xerox 2) print (data) # output: [4, 1, 0, 1, 4, 9, 16]

The above code first creates an empty list, then adds new elements to the list through a for loop, and requires an if statement to ensure that x is in a certain range of values, which is a more common way to create.

The question is, the above creation requires several lines of code, so can it provide a concise way to create a list? List derivation plays this function.

Its schematic diagram is as follows:

Its grammar is divided into three parts:

(1) how many elements are required: the value of x is from-5 to 5, that is, 9 cycles are required.

(2) to judge whether a new element is inserted in this cycle: although a total of 9 cycles are required, it is not necessary to insert a new element in each cycle (the list does not have to contain 9 values). Each cycle needs to be judged according to certain conditions, such as whether the x of the current cycle is greater than-2, insert a new value if it is greater than-2, and do not insert a new value if it is less than-2.

(3) the value of the element: the current loop wants to insert a value that can be an expression that contains x or no x.

Now we can use the list derivation to do what we just did:

Data = [for x in range (- 5,5) if x > =-2] print (data) # output: [4, 1, 0, 1, 4, 9, 16]

The above three parts can be called: the number of cycles, the value you want to insert, and the condition for judging whether to insert or not.

2. How to use list derivation

The previous introduction mentioned that the list derivation consists of three parts, namely, the number of cycles, the value you want to insert, and the condition for judging whether or not to insert. Now, use these three sections to learn how to use three list deductions to create code with concise lists.

Topic: create a list with odd numbers less than 15

Analysis:

(1) number of cycles: 15

(2) the value you want to insert: odd

(3) judgment condition: whether it is odd or not.

So I wrote the following code:

The following values are obtained:

[1, 3, 5, 7, 9, 11, 13, 15]

This is the end of the content about "what is the principle of python list derivation and how to use it". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report