Mainly related to knowledge points
List is the most common type of data in our Python, we mainly have the following actions.
1. Index
2. Slicing
3. Append
4. Delete
5. Length
6. Loop (also called traversal)
7, including
8. Nesting
For example, define a list:
list2=[' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## '] or list2=list ([' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## ']
The basic methods involved and the introduction
A simple example:
I. Definition of a list
>>> list2=list ([' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## '] ) >>> Print (List2) [' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## '] or list2=[' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## '] we directly use the following definition, Testing and simulation of related methods and sections.
Second, slice and list value
Slicing is mainly for the method of getting some or a single element in the list, and the index is used to access the elements in the list, and the subscript is counted from 0.
Take a value from the list (by subscript, subscript starting from 0)
list2=[' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## ']print (list2[0]) Print (list2[1]) print (list2[-2]) results: openstackpython123 We find that the exact value of zero at the time of the value is the first element, better proof that the subscript is zero-based, and that print ( List2[-2]) happens to be the second-to-last element, which shows that the value can be reversed not only along the value.
Slices: Access the first multiple values inside: list2=[' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## '] Print (List2[0:3]) #取前三个 results: [' OpenStack ', ' python ', ' Linux '] try print (List2[:3]) results: [' OpenStack ', ' python ', ' Linux '] Thus we can see that the beginning of the subscript is 0, you can omit not to write, but also note that the subscript [0:3] can be taken to the elements of the three-way position, that is, the fourth position is not taken, we need to pay attention to the value of our boundary. Print (List2[3:-1]) #取不到最后一位print (list2[3:]) #取得第四位到最后一位print (List2[2:9:2]) #取出下标为第二位到下标为9之间的 every other element to take print once ( List2[-3:-1]) #倒着取值取出倒数第三位和倒数第二位print (list2[-3:]) #取出倒数的后三位print (List[0::2]) #每隔一个元素取一次print (List[::2]) #每隔一个元素取一次 The same results as above: [' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 '] [' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' WW 33## ' [' Linux ', ' Zabbix ', ' Linux ', ' 123 ' [' Linux ', ' 123 ' [' Linux ', ' 123 ', ' ww33## ']
Third, add elements to the list
The main use of Append method to append elements, at the end of the increase
List2.append ("winner") print (LIST2) results: [' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' Linux ', ' Linux ', ' 123 ', ' ww33## ', ' winner ']
To insert an element in a location, you can use the Insert method to implement
List2.insert (2, "Baidu") #在第三位插入元素baiduprint (LIST2) results: [' OpenStack ', ' python ', ' Baidu ', ' Linux ', ' Docker ', ' Zabbix ', ' Nginx ', ' 123 ', ' ww33## ') This shows that the position element moves backwards as it is inserted at a position.
Iv. Modifying the value of an element
Modifying the value of an element is typically achieved by re-assigning it.
List2[2]= "My Steel" list2[-1]= "Life is short, I use python!" #List2. Insert (2, "Baidu") print (LIST2) [' OpenStack ', ' python ', ' My Steel ', ' Docker ', ' Zabbix ', ' nginx ', ' 123 ', ' Life is short ', I use python! ']
V. Delete
Delete is divided into delete one and delete all, at the time of deletion, we can use the Remove () method and the Pop () method, with the Del and clear () method can delete the entire list. Next, we analyze the difference between several methods according to the example.
The parameters in the middle of the pop () method are subscript, and the last one is deleted by default without parameters.
List2.pop (1) print (LIST2) results [' OpenStack ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' 123 ', ' ww33## ']list2.pop ( -4) print ( LIST2) Results: [' OpenStack ', ' python ', ' Linux ', ' Docker ', ' nginx ', ' 123 ', ' ww33## ']
The parameter in the middle of the Remove () method is directly the contents of the element
List2.remove (' Linux ') print (LIST2) results: [' OpenStack ', ' python ', ' Docker ', ' Zabbix ', ' nginx ', ' 123 ', ' ww33## '] When the list has the same elements as Linux: list2=[' OpenStack ', ' Linux ', ' Python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' 123 ', ' Linux ', ' ww33## '] List2.remove (' Linux ') print (LIST2) results: [' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' 123 ', ' Linux ', ' ww33## '] Only one value is deleted when there are multiple values
The clear () method is used, which is the processing of the entire list
List2.clear () print (LIST2) results: []
Del can delete an entire list or a single element
Del list2print (List2) del list[2] Results: Traceback (most recent call last): File "c:/users/administrator/ pycharmprojects/s14/jiounumber.py ", line +, in <module> print (List2) nameerror:name ' List2 ' are not Defined delete individual elements combined with slices list2=[' openstack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## ']del list2[2]print (LIST2) Results: [' OpenStack ', ' cloud computing ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## '
As you can see, Del is the list of deleted definitions, and clear () is the element that clears the list.
Vi. querying the location of an element and the number of occurrences (list elements can be duplicated)
The query for a list mainly has a statistical list of how long and where elements of a content are queried, primarily using the index () method and the count () element occurrences method implementation.
list2=[' OpenStack ', ' python ', ' Linux ', ' Docker ', ' Zabbix ', ' nginx ', ' 123 ', ' ww33## '] index: print (List2.index ("Linux)") Result 2 Stats: Print (List2.count ("Linux")) Results 1 list2=[' OpenStack ', ' Linux ', ' Python ', ' Linux ', ' Docker ', ' Zabbix ', ' Linux ', ' Nginx ' , "123", "ww33##"]print (List2.index ("Linux")) Print (List2.count ("Linux")) Results: 13
As a result, when an element contains multiple elements of the same shape, the index is generally the first one to find the element.
Vii. flipping and sorting the list
You can use the reverse () method and the sort () method to flip and sort the list, so-called Flip, the position of the list element is reversed, and the sort is sorted mainly by Ascall code.
list2=[' OpenStack ', ' Linux ', ' Python ', ' Linux ', ' Docker ', ' Zabbix ', ' Linux ', ' nginx ', ' 123 ', ' ww33## ']list2.reverse () Print (LIST2) results: [' ww33## ', ' 123 ', ' Nginx ', ' Linux ', ' Zabbix ', ' Docker ', ' Linux ', ' Python ', ' Linux ', ' OpenStack ' sort: List2 . Sort () print (LIST2) [' 123 ', ' Docker ', ' Linux ', ' Linux ', ' Linux ', ' Nginx ', ' OpenStack ', ' python ', ' ww33## ', ' Zabbix '] A little more complicated, inside add Chinese and integer content such as list2=[' OpenStack ', ' cloud computing ', ' Python ', 123, ' Docker ', ' Zabbix ', ' Linux ', 30.84, ' 123 ', ' ww33## '] List2.sort () print (LIST2) execution Result: Traceback (most recent call last): File "c:/users/administrator/pycharmprojects/ s14/jiounumber.py ", line, in <module> list2.sort () typeerror:unorderable types:int () < str () integer and string cannot be compared , Next, try List2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', ' 123 ', ' ww33## ']list2.sort () print (LIST2) [' 123 ', ' Linux ', ' OpenStack ', ' python ', ' ww33## ', ' Middle East ', ' China ', ' cloud computing '
This finds that the sort of list is still very NB, we can convert the shape to a string comparison about the integer type above and the string cannot be compared.
Viii. loops, inclusions, and copy (), Extend () methods
Loops, which are generally the way to print the elements of a list by a for loop
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', ' 123 ', ' ww33## ']for i in List2: print (i) Print ( LIST2) Results: OpenStack Cloud computing python China Middle East linux123ww33##[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', ' 123 ', ' ww33## '] By looping we can find each element of the list. At the same time, we can see that the print is different from the previous
Contains: We can determine whether a content is in the list, the result of the return value is a Boolean value ture or FALSE, the existence of a true non-existent false.
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## ']print (' linuxww ' in List2) print (' Middle East ' in List2 ) print (123 in List2) print (LIST2) results: Falsetruetruefalse
The entend method uses this method to merge two lists into one, with no effect on one of the values.
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## ']list1=[123, ' abc ', ' China '] #print (' Linuxww ' in LIST2) #print (' Middle ' in List2) print (list2,list1) print (List1) list1.clear () print (LIST1) results: [' OpenStack ', ' cloud computing ', ' python ' , ' China ', ' Middle East ', ' Linux ', 123, ' ww33## '] [123, ' ABC ', ' China '][123, ' abc ', ' China '] [] it is found that the Extend method does not affect the value of the merged list (LIST1), only if you do other things yourself. Changed.
Copy () method
Replication of the list can be implemented
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## ']list3=list2.copy () print (LIST2,LIST3) results: [' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' ww33## ' Note: list2=[' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ']list3=list2.copy () print (List2 Print (LIST3) list2[2]= "MYSQl DBA" list2[3][1]= "Beijing" Print (List2) print (LIST3) execution result: [' OpenStack ', ' cloud computing ', ' python ', [' China ' , ' Middle East ', ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' MYSQl DBA ', [' China ', ' Beijing '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Beijing '], ' Linux ', 123, ' ww33## '] we will list List2 in list2[2] of Python and list2[3][1] the Middle East changed to list2[2]= "MYSQl DBA" and list2[3][1]= "Beijing output after discovering that part of the copy list2[3][1" after copying is the same, The value of list2[2] will be different. The main reason is the memory address. At the same time for this situation want to replicate the same is not list2=[' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ']list3=list2print (LIST2) Print (LIST3) list2[2]= "MYSQl DBA" list2[3][1]= "Beijing" Print (List2Print (LIST3) assigns a value of List2 here to List3 when the change List2 is changed, the value of LIST3 is changed directly [' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' MYSQl DBA ', [' China ', ' Beijing '] , ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' MYSQl DBA ', [' China ', ' Beijing '], ' Linux ', 123, ' ww33## ') to copy exactly the same, we can import the Copy module imp Ort copylist2=[' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ']list3=copy.deepcopy (List2) print ( LIST2) print (LIST3) list2[2]= "MYSQl DBA" list2[3][1]= "Beijing" Print (List2) print (LIST3) execution results: [' OpenStack ', ' cloud computing ', ' python ' , [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' Cloud computing ', ' MYSQl DBA ', [' China ', ' Beijing '], ' Linux ', 123, ' ww33## ' [' OpenStack ', ' cloud computing ', ' python ', [' China ', ' Middle East '], ' Linux ', 123, ' ww33## It is found that the value change of the LIST2 element does not affect the value of LIST3, the following copy, which we call deep.copy, and the copy () method of the list is a shallow copy
Ix. Other operations of the list
In addition to the main operations above, we can also do the following on the list. The operands of the list to + and * are similar to strings. + sign for combined list, * number for repeating list
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123]list3=[' open ', ' ip ', ' config ']print (list2+list3) print ( LIST3*3) Results: [' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' Open ', ' ip ', ' config ' [' Open ', ' ip ', ' config ', ' open ', ' ip ', ' config ', ' open ', ' ip ', ' config ']+ the function is the same as the previous append (), while * means repetition, followed by a number indicating the number of repetitions.
Python list functions
In the above we analyzed the Python list method, the Python list also has some list functions, the common has the MAX (list), and the Len (list) and the list (seq):
The Len (list) is the length of the list mentioned above,
Max (list) value
Min value of min (list) List
List (seq) converts tuples to lists, and we know that the basic attributes of tuples are the same as lists, except that the list is defined using [] square brackets, the tuple is () the parentheses are defined, and the elements of the tuple cannot be re-assigned.
Example
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123]list3=[' open ', ' ip ', ' config ', ' 999 ', ' 2929 ',]tet= ("www", " www ", 1234) print (Type (TET)) Print (list (TET)) Print (list (TET)) print (LIST2+LIST3) print (list3*3) print (Len ( LIST3) print (max (LIST3)) print (min (list3)) Results: <class ' tuple ' > #元组tet的类型 [' www ', ' www ', 1234] #print (list ( TET)) tuple conversion list output <class ' list ' > #print (List (TET)) tuple conversion list after type validation [' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', ' Linux ', 123, ' Open ', ' ip ', ' config ', ' 999 ', ' 2929 ' [' Open ', ' ip ', ' config ', ' 999 ', ' 2929 ', ' Open ', ' ip ', ' config ', ' 999 ', ' 2929 ', ' Open ', ' ip ', ' config ', ' 999 ', ' 2929 ']5 #print (len (list3)) List List3 length statistics The maximum value of the Open #print (Max (LIST3)) List 2929 #print (min (list3)) List minimum value
X. Nesting of Lists
When we refer to the copy () method of the list above, we use the list because the elements of the list can be of any type, so the elements of the list can also be lists, and then the list will form a nested relationship.
For example:
list2=[' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', [[' Open ', ' IP '], ' config ', ' 999 ', ' 2929 ',]]print (LIST2) Results: Implement three levels of nesting [' OpenStack ', ' cloud computing ', ' python ', ' China ', ' Middle East ', [[' Open ', ' IP '], ' config ', ' 999 ', ' 2929 ']
Summary: The above content is mainly python in the operation of the list, all of the basic knowledge, and all the examples are the results of hands-on experience, because of inexperience, may only understand the list of knowledge part, where there are errors, but also ask your friends to correct me.