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

Example Analysis of python list Generation

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of python list generation. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

List generation

As the name implies, a list generator is an expression in a specific grammatical form used to generate a list.

For example, if we need to generate a list of 0 to 9, we can write:

List1 = [0Perry 1, 2, 3, 4, 5, 6, 7, 8, 9]

It feels good to write these 10 numbers manually, but sometimes, testing algorithms or functions, is it too tired to write them manually? Is there a more convenient way.? Yes。

First of all, let's introduce range.

What is the function of range?

The range () function creates a list of integers, which is typically used in for loops.

Function syntax 
 range (start, stop [, step])

Parameter description:

Start: the count starts with start. The default is 0. For example, range (5) is equivalent to range (0,5)

Stop: count to the end of stop, but does not include stop. For example, range (0,5) is [0,1,2,3,4] without 5

Step: step size. Default is 1. For example: range (0,5) is equivalent to range (0,5,1) 
 instance

Range (10) # from 0 to 10

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Range (1,11) # from 1 to 11

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Range (0,30,5) # step size is 5

[0, 5, 10, 15, 20, 25]

Range (0,10,3) # step size is 3

[0, 3, 6, 9]

Range (0,-10,-1) # negative number

[0, 1,-2,-3,-4,-5,-6,-7,-8,-9]

Range (0)

[]

Range (1,0)

[]

By using range, you can quickly generate a sequential list

Then, it's not that simple. 
 what if I need to do special treatment for each element in the list, such as addition and subtraction?

Do it again with for.

Draw the key points. Use list generation.

Basic grammar format 
 [exp for iter_var in iterable]

Working process:

Iterate over each element in the iterable

In each iteration, the result is assigned to iter_var, and then a new calculated value is obtained through exp.

Finally, all the calculated values obtained through exp are returned as a new list.

It's the equivalent of this process:

L = []

For iter_var in iterable:

L.append (exp)

Circular nested syntax format 
 [exp for iter_var_A in iterable_A for iter_var_B in iterable_B]

Working process:

For each element in the iterative iterable_A, all the elements in the ierable_B are iterated.

It's the equivalent of this process:

L = []

For iter_var_A in iterable_A:

For iter_var_B in iterable_B:

L.append (exp)

This is the end of this article on "sample analysis of python list generation". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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