Python data structure (2) ------ list, python Data Structure
This article will focus on the list and list operations.
2.1 list Functions
2.2 basic list operations
2.3 List Method
2.1 list Functions
>>> List ('hello ')
['H', 'E', 'l', L', 'O']
Note: The list function applies to all types of sequences, not just strings.
2.2 basic list operations
2.2.1 changing the list: Element assignment
>>> X = [1, 3, 5]
>>> X [0] = 'A'
>>> X
['A', 3, 5]
Note: you cannot assign values to nonexistent elements, that is, x [0] ~ X [2] exists and can be assigned a value. x [3] does not exist and cannot be assigned a value.
2.2.2 deleting an element
>>> Names = ['jack', 'bob', 'Tom ', 'Alice']
>>> Del names [1]
>>> Names
['Jack', 'Tom ', 'Alice']
2.2.3 multipart assignment
>>> Name = 'Anthony'
>>> Namelist = list (name)
['A', 'n', 't', 'h', 'O', 'n', 'y']
# When the step size is 1, replace the original part location with the value assignment list
>>> Namelist [1: 3] = ['A', 'B', 'C']
['A', 'A', 'B', 'C', 'h', 'O', 'n', 'y']
# When the step size is 2, the length of the value assignment list must be consistent with the number of original partition Elements
>>> Namelist [: 2] = [1, 2, 3]
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
ValueError: attempt to assign sequence of size 3 to extended slice of size 4
>>> Namelist [: 2] = [1, 2, 3, 5]
>>> Namelist
[1, 'A', 2, 'C', 3, 'O', 5, 'y']
# The multipart operation can delete sequences.
>>> Namelist [1:] = []
>>> Namelist
['A']
2.3 List Method
2.3.1 append
# Append method append a new object to the end of the list;
>>> Lst = ['A', 'B', 4]
>>> Lst. append ('A ')
['A', 'B', 4, 'a']
# Directly modify the original list
2.3.2 count
# Count the number of times an element appears in the list;
>>> Lst. count ('A ')
2
2.3.3 extend
# The extend method appends multiple values in another sequence at the end of the list;
>>> Lst2 = [1, 2, 3]
>>> Lst. extend (lst2)
>>> Lst
['A', 'B', 4, 'A', 1, 2, 3]
# Unlike cascade operations, the extend method directly modifies the original list, and the "+" Operation returns a new list.
>>> A = ['A', 'A', 'B']
>>> B = [1, 2, 3]
>>> A + B
['A', 'A', 'B', 1, 2, 3]
>>>
['A', 'A', 'B']
2.3.4 index
# The index method is used to find the index location of the first matching item of a value in the list;
>>> Names = ['jack', 'Tom ', 'Alice', 'Rose ', 'timor']
>>> Names. index ('jack ')
0
>>> Names. index ('Sam ')
Traceback (most recent call last ):
File "<stdin>", line 1, in <module>
ValueError: 'Sam 'is not in list
2.3.5 insert
# The insert method is used to insert objects into the list;
>>> Numbers = [1, 2, 3, 4, 5, 6, 7]
>>> Numbers. insert (3, 'A ')
>>> Numbers
[1, 2, 3, 'A', 4, 5, 6, 7]
# You can also perform the multipart operation.
>>> Numbers [3] = ['a']
[, 3, 'A', 'A',]
2.3.6 pop
# The pop method removes an element from the list (the last one by default );
>>> X = [1, 2, 3]
>>> X. pop ()
3
>>> X
[1, 2]
>>> X. pop (0)
1
>>> X
[2]
2.3.7 remove
# The remove method is used to remove the first matching item of a value in the list;
>>> X = ['Alice ', 'tome', 'time', 'jack']
>>> X. remove ('Alice ')
[Tome ', 'time', 'jack']
2.3.8 reverse
# The reverse Method reversely stores the elements in the list;
>>> X = [1, 3, 5]
>>> X. reverse ()
>>> X
[5, 3, 1]
2.3.9 sort
# Sort the sort method in the original position;
>>> X = [4, 6, 9, 7, 8]
>>> X. sort ()
>>> X
[4,6, 7,8, 9]
# The sort method has no return value. Therefore, the return value of y = x. sort () is None.