One, list operation mind Map
Two, list operation detailed explanation
1, definition and initialization
LST = list () #使用list定义一个空列表
LST = [] #使用 [] Define an empty list
LST = [1,2,3] #使用 [] Define and initialize the list
LST = List (range (10)) Use the list function to convert an iterative object to a list
usually use [] when you define a list, and use the list function when converting to a list of iterated objects
2, access to list elements, accessed by subscript (also called index)
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> Lst[0]
0
>>> LST[10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Indexerror:list index out of range
>>> Lst[-1]
9
>>> Lst[-11]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Indexerror:list index out of range
Indexerror will be reported when the index is out of range, positive index and negative index will be reported
Find index by value, List.index () contains three parameters, Value,start,stop, select according to need
Index (...)
L.index (value, [Start, [stop]])-> integer--Return the value of the.
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst.index (5)
5
The start parameter specifies which direction to start the lookup, the stop parameter specifies which index to end from, and does not include the index, and throws ValueError when the value does not exist
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst.index (5,1,4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Valueerror:5 is isn't in list
>>> Lst.index (5,1,6)
5
>>> Lst.index (5,1,5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Valueerror:5 is isn't in list
>>>
Start and stop can be negative, but always look from left to right, stop must start big, or error
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst.index (5,-4,-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Valueerror:5 is isn't in list
>>> Lst.index (5,-6,-1)
5
3,list.count () returns the number of elements in the list
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> Lst.count (9)
1
4, modify the elements in the list
>>> LST
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[3]=5
>>> LST
[0, 1, 2, 5, 4, 5, 6, 7, 8, 9]
Modifying an element only this method
4, add elements to the list
add element, cannot use index operation
List.append (x) adds an element to the end of the list
List.insert (2,11) adds an element of 11 before an element indexed to 2
>>> LST
[0, 1, 2, 5, 4, 5, 6, 7, 8, 9]
>>> Lst.append (10)
>>> LST
[0, 1, 2, 5, 4, 5, 6, 7, 8, 9, 10]
>>> Lst.insert (9,100)
>>> LST
[0, 1, 2, 5, 4, 5, 6, 7, 8, 100, 9, 10]
Lst.extend () can add an iterative object to the end of the list, extend cannot add a single element, it must be an iterative object
>>> LST = [1,2,3,4,5]
>>> lst.extend (Range (3))
>>> LST
[1, 2, 3, 4, 5, 0, 1, 2]
5, delete element
List.remove (value) from left to right, deletes the first element
List.pop () does not pass the index parameter, deletes the end element, passes the index argument, deletes the element indexed as index, and returns the deleted element
List.clear () empty list
>>> LST
[1, 2, 3, 4, 5, 0, 1, 2]
>>> Lst.remove (2)
>>> LST
[1, 3, 4, 5, 0, 1, 2]
>>> Lst.pop ()
2
>>> LST
[1, 3, 4, 5, 0, 1]
>>> Lst.pop (0)
1
>>> LST
[3, 4, 5, 0, 1]
Lst.clear () Not in Python 2.x, 3.x version only
6, other operations
Len (list) Print List length
List.reverse () reverses the list, = = Lst[::-1] Slices can also be reversed
List.sort () uses a quick Sort method
LST 2 = LST copy, Lst2 and LST are pointing to the same piece of memory
>>> Lst1 = LST
>>> Lst1
[3, 4, 5, 0, 1]
>>> ID (LST)
58316296L
>>> ID (lst1)
58316296L
The following content Python2 not supported
Lst.copy () Shallow copy (shadow copy), the copy operation is passed a reference, copy operation, the variable object is a reference to pass, to immutable objects is value
Deep copy, import copy module required
Import Copy
Lst2 = copy.deepcopy (LST)
At this time Lst2 and LST memory is not a piece of memory