Copy List
Shallow copy: simple type element full copy, copy only the elements cited reference type
L1 = [3,2,1,[4,5,6],8,'abc'] L1 [3, 2, 1, [4, 5, 6], 8, 'abc'] L2 = L1.copy() L2 [3, 2, 1, [4, 5, 6], 8, 'abc']
L1 [. 3] [. 1] 10 = # elements L1, L2 modification also changes L1 [ . 3, 2,. 1, [. 4, 10,. 6],. 8, ' ABC ' ] L2 [ . 3, 2,. 1, [. 4, 10,. 6],. 8, ' ABC ' ]
Deep copy: copy module provides deepcopy, reference types complete copy of the new element
import copy L3 = copy.deepcopy(L1) L3 [3, 2, 1, [4, 10, 6], 8, 'abc']
L1 [. 3] [. 1] = 20 is L1 [ . 3, 2,. 1, [. 4, 20 is,. 6],. 8, ' ABC ' ] L3 # L3 is a deep copy of the list of newly generated modified L1 element has no effect on the L3 [ . 3, 2,. 1, [. 4, 10,. 6],. 8, ' ABC ' ]
*: Make reference to the type shallow copy process
L4 = [[1,2,3]] * 3 L4 [[ 1, 2, 3], [1, 2, 3], [1, 2, 3 ]] L4 [ 1] [1] = 10 L4 [ [ 1, 10, 3], [1, 10, 3], [1, 10, 3]]
for X in L4: Print (X) Print (ID (X)) # fetch memory address [1, 10, 3 ] 84289032 [ 1, 10, 3 ] 84289032 [ 1, 10, 3 ] 84289032 # same memory address
L5 = [[1, 2, 3], [1, 2, 3], [1, 2, 3]] L5 [[1, 2, 3], [1, 2, 3], [1, 2, 3]] for x in L5: print(x) print(id(x)) [1, 2, 3] 87157000 [1, 2, 3] 84213512 [1, 2, 3] 87157128
List Removing elements
remove (value) based on the elements to find, remove the first element arise, and will lead to a list of elements moving O (n)
L6 = [3,2,1,4,5,6,7,2,3,4] L6 [3, 2, 1, 4, 5, 6, 7, 2, 3, 4] L6.remove(2) L6 [3, 1, 4, 5, 6, 7, 2, 3, 4] L6.remove(10) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-31-8cf95df45268> in <module> ----> 1 L6.remove(10) ValueError: list.remove(x): x not in list
pop (index) based on the index lookup, remove index bit element O (1), and causes movement of the element list O (n)
L6.pop(3)
5 L6 [3, 1, 4, 6, 7, 2, 3, 4] L6.pop(10) L6 --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-33-8663c8410c3d> in <module> ----> 1 L6.pop(10) 2 L6 IndexError: pop index out of range
pop () to remove the tail of a list element O (1), high efficiency
L6.pop()
4
L6
[3, 1, 4, 6, 7, 2, 3]
clear () to clear the list of all the elements, that the length of the list is 0, the reference count is decremented element, leaving an empty list
L6.clear()
L6
[]
Flip list
reverse () Reverse the list of elements, modify itself
L7 = [3,2,1,4,5,6,7,2,3,4] L7 [3, 2, 1, 4, 5, 6, 7, 2, 3, 4] L7.reverse() L7 [4, 3, 2, 7, 6, 5, 4, 1, 2, 3]
List sorted
sort (key = function, reverse = True) sort the list element, the default ascending modifies itself
specify how different types of key elements sorted in the sort only, without modifying the elements
reverse = True descending sorting
L8 = [3,2,1,4,5,6,7,2,3,4,'a','b','c'] L8 [3, 2, 1, 4, 5, 6, 7, 2, 3, 4, 'a', 'b', 'c'] L8.sort() --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-46-951f259be954> in <module> ----> 1 L8.sort() TypeError: '<' not supported between instances of 'str' and 'int' L8.sort(key=str,reverse=True) L8 ['c', 'b', 'a', 7, 6, 5, 4, 4, 3, 3, 2, 2, 1]
Analyzing element is present
in determining whether there is an element in the list, return bool value
'c' in L8 True 4 in L8 True 10 in L8 False