dir(list)
View the built-in functions contained in the list via
1. append to add elements
list.append(obj)
list += [obj]
same as
a = [1, 2, 3]
a += [4]
a.append(5)
print(a)
>>> [1, 2, 3, 4, 5]
2. clear to clear the list
list.clear()
list = []
same as
a = [1, 2, 3]
a.clear()
print(a)
>>> []
3. copy copy
Involving shallow copy and deep
copy Shallow copy is as follows b = a
. After copying, a and b have the same address and point to the same memory space. If one of the internal data is modified, the other will also be modified together (including the shallow copy being modified when passing the function).
The deep copy is as follows c = a.copy()
, at this time c will open up a new memory space to store the data in a, but it can be found that although the addresses of a and c are different, modifying the first three data does not affect each other, but a[3] and c[3] The addresses are the same, modifying the data in the second-level list of the fourth item will still affect each other.
Personal understanding: Although c has opened up a new memory space to store data, but a[3] itself stores the address of this second-level list, so a[3] and c[3] point to the same address, and it is natural to modify the value will affect each other. Using copy.deepcopy()
can make the inner layer also fully deep copy.
import copy
a = [1, 2, 3, [4, 5, 6]]
b = a
c = a.copy()
d = copy.deepcopy(a)
print('a: ', id(a))
print('b: ', id(b))
print('c: ', id(c))
print('a[3]: ', id(a[3]))
print('c[3]: ', id(c[3]))
print('d[3]: ', id(d[3]))
>>>
a: 140296574785088
b: 140296574785088
c: 140296574786368
a[3]: 140296574785248
c[3]: 140296574785248
d[3]: 140296574784528
4. count count
a = [[1, 2, 3], [1, 2, 3]]
print(a.count([1, 2, 3]))
>>> 2
5. extend adds iterable objects
list.extend(Iterable)
list += Iterable
same as
a = [1, 2, 3]
a.extend([1, 2, 3, [4, 5, 6]])
print(a)
>>> [1, 2, 3, 1, 2, 3, [4, 5, 6]]
6. index Get the subscript of the element
When there are repeated elements, only the subscript of the first element is returned, and an error is reported directly if the element does not exist.
a = [1, 2, 3, 1]
print(a.index(1))
>>> 0
7. insert Add elements at the specified position
a = [1, 2, 3]
a.insert(1, 10)
print(a)
>>> [1, 10, 2, 3]
8. pop deletes and returns the element at the specified position
a = [1, 2, 3]
print(a.pop(1), a)
>>> 2 [1, 3]
9. remove delete element
When there are duplicate elements, only the first element will be deleted, and an error will be reported directly if the element does not exist.
a = [1, 2, 3, 1]
a.remove(1)
print(a)
>>> [2, 3, 1]
10. reverse flip list
Inner layer does not flip
a = [1, 2, 3, [1, 2, 3]]
a.reverse()
print(a)
>>> [[1, 2, 3], 3, 2, 1]
11. sort sorting
reverse=bool
Control the sorting direction;
key
cooperate with lambda
the function to select the sorting basis, such as lambda x: x[1]
sorting by the size of the second element of each item, lambda x: len(x)
and sorting by the length of each item.
Note: a.sort()
When the first elements of two items are the same, the subsequent elements will be compared sequentially. [1,3]
The position is [1,7]
before , and a.sort(key=lambda x: x[0])
only the first element will be compared. If they are the same, they will be arranged in the original order.
a = [[2, 4], [1, 7], [2, 1], [1, 3]]
a.sort()
print(a)
a.sort(reverse=True)
print(a)
a.sort(key=lambda x: x[0])
print(a)
a.sort(key=lambda x: x[1])
print(a)
>>>
[[1, 3], [1, 7], [2, 1], [2, 4]]
[[2, 4], [2, 1], [1, 7], [1, 3]]
[[1, 7], [1, 3], [2, 4], [2, 1]]
[[2, 1], [1, 3], [2, 4], [1, 7]]