1. Defining the list
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']
The index (subscript) starts at 0, the "Xiaoyi" index position in the example is 0, and the index position of "Xiaoer" is 1
Extension: How to determine whether the current data type is a list format? You can use the Python built-in type method to make judgments, using the following methods
Type (a) is List
Judged by the Boolean value returned, true Yes, Flase no
2. Adding and deleting changes
Check:
The first element in the output list print (A[0])
All contents of output list: print (a[0:])
#以下为列表切片查询
Everything except the last element in the output list: print (A[0:-1])
From left to right in the output list, take one of the values: print (A[0::2]) #步长为2
All content in the output list, right-to-left values: print (A[::-1])
Right-to-left values in the output list, one for each value: print (A[::-2])
Fixed position takes a value from right to left, starting with ' Xiaoba ': print (A[7::-2])
Increase:
#两种方法向列表中增加数据: Insert/append
The difference between the two methods: Append is to append the element directly to the last of the list, insert can specify the element position, insert the element into any specified index
Append instance: inserting ' Xiaoshi ' into the last of a list
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a.append ('Xiaoshi')Print(a)
Insert instance: inserting ' Xiaoshi ' into the second bit of a list
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a.insert (1,'Xiaoshi')Print(a)
Change:
There are no specific methods that can be modified by re-assignment.
Example 1, where the index value of 1 ' xiaoer ' is modified to ' Xiaoshi ':
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a[1]='Xiaoshi'Print(a)
Example 1, where the index value of 1 and 2 of the ' xiaoer ', ' Xiaosan ' batch modified to ' Xiaoshi ', ' Xiaoshiyi ':
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a[1:3]=['Xiaoshi','Xiaoshiyi']Print(a)
By deleting:
#三种方法删除列表中的数据: Remove/pop/del
The difference between the three methods: remove is to delete the data through the element, the pop is deleted by the index, and the pop is able to return the deleted value. Del is a python built-in Delete method that can be used to delete data from the list, as well as to delete other forms of data. Here del can all be used to delete the entire a variable
Remove method instance: Remove ' xiaoer ' from the list
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a.remove ('Xiaoer')Print(a)
Pop method instance: delete ' Xiaoer ' in the list and use variable B to receive deleted object values, print B to view deleted data
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']b=a.pop (1)Print(a)Print(b)
#注:p op method if you do not specify a delete location, the last column value of the list is deleted by default.
Del method instance: Remove ' xiaoer ' from the list
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']delA[1]Print(a)
#注: Remove and pop can only delete a single value, and Del can delete multiple
Extension: Use the Clear method to clear the contents of the list, using the method: A.clear ()
Other ways to list:
1.count: Used to count the number of occurrences of an element in a list
a=['xiaoyi','xiaoer','xiaosan' ,'xiaosi','xiaowu'].count (' Xiaoyi ' )print(a)
2.extend: Used to append data from a list to the original list (extended)
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi']b=['Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a.extend (b)Print(a)
3.index: Used to know the value of an element, to find an index using an element value
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']Print (A.index ('Xiaosan'))
4.reverse: Invert the order of elements in the list as a whole
a=['Xiaoyi','Xiaoer','Xiaosan','Xiaosi','Xiaowu','Xiaoliu','Xaioqi','Xiaoba','Xiaojiu']a.reverse ()Print(a)
Execution results: [' xiaojiu ', ' Xiaoba ', ' Xaioqi ', ' Xiaoliu ', ' Xiaowu ', ' Xiaosi ', ' Xiaosan ', ' xiaoer ', ' Xiaoyi ']
5.sort: Sort the elements in the list from small to large in ASCII encoding
b=[1,3,4,8,2,9,7]b.sort ()print(b)
Results: [1, 2, 3, 4, 7, 8, 9]
Extension: Can the Sort method be sorted from large to small?
Yes, the Sort method has a remove condition built into it, the default Remove=flase, is a small to large sort, through the sort built-in method, when the sort method is called, the remove=true can be
b=[1,3,4,8,2,9,7]b.sort (Reverse=
Print (b)
Results: [9, 8, 7, 4, 3, 2, 1]
6. Query whether an element exists in the list
Two methods, 1 is the Count method mentioned earlier, if the list name. Count ("element name") result is 0, then this element is not in the list.
2 is a query using print (the "element name" in the list name), and the returned result is flase, indicating that the element is not in the list;
"Python Basics"-List-related basics