list —— It consists of a series of elements arranged in a specific order . stay Python in , In square brackets [] To represent a list , And use commas to separate the elements . as follows :
color = ['red','yellow','blue','blank','green']
print(color)
use Python Print out the list ,Python The internal representation of the list will be printed , Include brackets :
['red','yellow','blue','blank','green']
The following describes how to access list elements .
A list is an ordered set , Access list elements , Just tell the location or index of the element Python.
color = ['red','yellow','blue','blank','green']
print(color[0])
function , Output :
red
in addition , The index is zero based , The index of the first element is 0, The index of the second element is 2,N A for N-1. stay Python A special syntax for accessing the last list element is provided in . Specify the index as -1, Allowing Python Returns the last list element :
>>>color = ['red','yellow','blue','blank','green']
>>>print(color[-1])
green
Again , Indexes -2 Returns the penultimate element , Indexes -3 Returns the penultimate element , And so on .
The values in the list can be used just like any other variable .
>>>color = ['red','yellow','blue','blank','green']
>>>message = "My favorite color is " + color[2] + '.'
>>>print(message)
My favorite color is blue
You can use splicing to create messages based on the values in the list .
Modify list elements , By specifying the list name and the index of the element to be modified , When specifying the new value of this element .
>>>color = ['red','yellow','blue','blank','green']
>>>print(color[0])
red
>>>color[0] = 'white'
>>>print(color[0])
white
>>>print(color)
['white','yellow','blue','blank','green']
We can see that , The value of the first element changes , The following elements have not changed , We can modify the values of each element in the list by assigning new values .
Add elements to the list :
Add elements to the list , The simplest thing to do is add at the end of the list , Use append() Add elements to the end of the list :
>>>color = ['red','yellow','blue','blank','green']
>>>print(color)
['red','yellow','blue','blank','green']
>>>color.append('white')
>>>print(color)
['red','yellow','blue','blank','green','white']
so , We use append() It's easy to put new elements ‘white’ Add to the end of the list , We can also create an empty list first , Use when necessary append() Statement to add elements .
Insert elements in the list :
Usage method insert() You can add a new element anywhere in the list .
>>>color = ['red','yellow','blue','blank','green']
>>>print(color)
['red','yellow','blue','blank','green']
>>>color.insert(1,'white')
>>>print(color)
['red','white','yellow','blue','blank','green']
This operation moves the element added to the position and the following elements in the list to the right by one position .
Remove elements from the list :
1. Use del Statement delete element :del Element can delete elements anywhere , After deletion, you cannot access .
>>>color = ['red','yellow','blue','blank','green']
>>>print(color)
['red','yellow','blue','blank','green']
>>>del color[0]
>>>print(color)
['yellow','blue','blank','green']
2. Usage method pop() Remove elements : Method pop() You can delete the elements at the end of the list , And can continue to use .
>>>color = ['red','yellow','blue','blank','green']
>>>print(color)
['red','yellow','blue','blank','green']
>>>color_pop = color.pop()
>>>print(color)
['red','yellow','blue','blank']
>>>print(color_pop)
green
In the above procedure , Let's create a new variable color_pop To store pop() The last element of the pop-up list green, therefore , Method of use pop() Remove elements , We can still access the deleted values . actually , We can use pop() To delete elements anywhere in the list , Just specify the index of the element to be deleted in the index , for example :
>>>color = ['red','yellow','blue','blank','green']
>>>color_pop = color.pop(1)
>>>print(color)
['red','blue','blank','green']
>>>print(color_pop)
yellow
therefore , ad locum , We can define these two methods in this way : If you want to remove an element from the list and no longer use it in any way , Just use del sentence ; If you want to continue using the element after deleting it , Just use the method pop().
3. Delete elements... Based on values : If we don't know where to delete the element , Only know the value to delete , Serviceable method remove()
>>>color = ['red','yellow','blue','blank','green']
>>>print(color)
['red','yellow','blue','blank','green']
>>>color.remove('blue')
>>>print(color)
['red','yellow','blank','green']
Method remove() Delete only the first specified value , If the value to be deleted may appear more than once in the list , You need to use a loop to determine if all of these values have been deleted .
1. Usage method sort() Sort the list permanently : Method sort() You can sort the elements in the list alphabetically , But first, we should unify the case of letters .
>>>color = ['red','yellow','blue','blank','green']
>>>color.sort()
>>>print(color)
['blank', 'blue', 'green', 'red', 'yellow']
This change in order is a permanent change in the order in which the list elements are arranged . You can also arrange the list elements in reverse alphabetical order , Just to sort() Method pass parameter reverse=True.
>>>color = ['red','yellow','blue','blank','green']
>>>color.sort(reverse=True)
>>>print(color)
['yellow', 'red', 'green', 'blue', 'blank']
You can use functions sorted Sort the list temporarily , function sorted() Enables you to display list elements in alphabetical order , At the same time, it does not affect their order in the original list . Again , Arrange in reverse alphabetical order , Is also a directional function sorted() Pass parameters reverse=True.
2. Print the list backwards :reverse() Reverse the order of the list elements , Call again to restore the list order .
>>>color = ['red','yellow','blue','blank','green']
>>>color.reverse()
>>>print(color)
['green', 'blank', 'blue', 'yellow', 'red']
# Call again reverse(), The list order returns to the original order
>>>color.reverse()
>>>print(color)
['red','yellow','blue','blank','green']
3. Determine the length of the list :len(), Use len() You can quickly get the length of the list .
>>>color = ['red','yellow','blue','blank','green']
>>>len(color)
5
Python Calculate list elements from the beginning , So when determining the length of the list , There will be no one - to - one error .len() It is useful to , We can use it to determine how many elements are in a list , How many visualizations need to be managed , How many registered users .