The Python list is a commonly used computer language. It is widely used. The following article introduces it in detail, I hope you will have a better understanding of your Python list by reading this article. Here we will introduce the relevant content and hope to help you.
List is the data structure that processes a group of ordered projects. You can store a series project in a list. Suppose you have a shopping list that records the items you want to buy, so you can easily understand the list. However, in your shopping table, each item may have a single row, while in Python, you use commas to separate each item.
The items in the list should be included in square brackets so that Python knows that you are specifying a list. Once you create a list, you can add, delete, or search for projects in the list. Since you can add or delete a project, we say that the list is a variable data type, that is, this type can be changed.
- Python programming language user experience
- Differences between the Python programming language and other languages
- Effect of using the Python programming language in Java
- Python command line code Enhanced memory
- Convert Python print encoding to default encoding
Quick Start to objects and Classes
Although I have been postponing the discussion of objects and classes, a little explanation of them can help you better understand the list. We will explore this topic in detail in the corresponding chapter.
List is an example of using objects and classes. When you use variable I and assign values to it, for example, if you assign an integer 5, you can think that you have created a class type) int object instance) I. In fact, you can take a look at help (int) to better understand this.
Classes also have methods, that is, they define local functions for classes only. You can use these functions only when you have an object of this class. For example, Python provides the append method for the list class. This method allows you to add a project at the end of the list. For example
- mylist.append('an item')
Add the string to the mylist. Note: Use the dot to use the object method.
A class also has a domain, which is a variable defined only for the class. Only when you have an object of this class can you use these variables/names. Class, for example
- mylist.field。
Quick Start to objects and Classes
- #!/usr/bin/python
- # Filename: using_list.py
-
- # This is my shopping list
- shoplist = ['apple', 'mango', 'carrot', 'banana']
-
- print 'I have', len(shoplist),'items to purchase.'
-
- print 'These items are:', # Notice the comma at end of the line
- for item in shoplist:
- print item,
-
- print '/nI also have to buy rice.'
- shoplist.append('rice')
- print 'My shopping list is now', shoplist
-
- print 'I will sort my list now'
- shoplist.sort()
- print 'Sorted shopping list is', shoplist
-
- print 'The first item I will buy is', shoplist[0]
- olditem = shoplist[0]
- del shoplist[0]
- print 'I bought the', olditem
- print 'My shopping list is now', shoplist
-
Output
- $ python using_list.py
- I have 4 items to purchase.
- These items are: apple mango carrot banana
- I also have to buy rice.
- My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
- I will sort my list now
- Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
- The first item I will buy is apple
- I bought the apple
- My shopping list is now ['banana', 'carrot', 'mango', 'rice']
-
How it works
The shoplist variable is a shopping list of someone. In shoplist, we only store the name string of the purchased item, but remember that you can add any types of objects including numbers or other lists to the list. We also use the for... in loop to recursively iterate between projects in the list. From now on, you must have realized that the list is also a sequence. The sequence features will be discussed in later sections. Note that a comma is used at the end of the print statement to remove the line breaks automatically printed by each print statement. This is a bit ugly, but it is simple and effective.
Next, we use the append method to add a project to the list, as we have discussed earlier. Then, we can print the list to check whether the project has been added to the list. To print a list, simply pass the list to the print statement, and we can get a clean output.
Next, we will use the sort method of the List to sort the list. It should be understood that this method affects the List itself, rather than returning a modified list-this is different from the method used to work with strings. This is what we call the list variable and the string variable.
Finally, we want to delete an item from the list when purchasing it in the market. We use the del statement to complete this task. Here, we point out which project in the list we want to delete, and the del statement is for us to delete it from the list. We specify that we want to delete the first element in the list, so we use del shoplist [0] to remember that Python starts counting from 0 ).
If you want to know all the methods defined by the list object, you can use
- help(list)
Obtain the complete Python list. I hope the above content will be helpful to you.