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 function of list list in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is to share with you about the role of list list in Python, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Create a list

Lists are represented by square brackets, elements are separated by commas, and this type of data is the Python list.

Another way to create a list is list generation, for example, to create an even list of less than 10. It may be easy to write 0meme 2, 4, 6 and 8 directly, but it may be troublesome if you want to generate 100 and less than 1000. So one of the advantages of using list generation is that the code is concise. The I in the code can be named arbitrarily and has no effect on the result.

What we need to talk about here is the range function, which is often used, and for range (x), the default starts at 0 and produces an integer list. You can also start with a specified number, or you can produce an isometric sequence with a specified difference.

Alternatively, you can use a for loop to traverse the list generated by the range function.

2. The number of the list

The list of python is numbered from 0 by default, that is, the index value of the first location is 0, for example:

List index has two methods, one is just said, starting from 0, according to the order of Arabic numerals, numbering in turn, and the other is starting from the tail, numbering from-1, starting with-1, and so on, when the index value exceeds the length of the list, it will report an indexError error.

Fetch the list element of the specified index value:

For training traverses the list of python:

List summation:

3. List's method

3.1 append

The append method is the most commonly used method to add elements to a specified list.

3.2 insert

The insert method is used to insert an element at a specified index location, such as:

When the inserted location index exceeds the length of the list, it is inserted at the last location by default.

3.3 extend

Just now the append method adds elements, and the extend method adds a new list at the end of the current list. For example, add list_2 to list_1:

3.4 index

The index method is used to view the index value of the element of list, such as:

3.5 remove

The translated words can be understood, that is, the meaning of deleting the element, and the parameter is the value of the element to be deleted.

3.6 sort

Sorting is important for list, but it's not very common, I think. Because for deep learning, the reading order of the data is not important, training that first and then that has no effect on the results of learning.

3.7 reverse

Reverse is used to flip a list, which is relatively simple, such as:

3.8 pop

The pop method is used to delete elements at the specified index location, such as:

The above is the role of the list list in Python, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report