List and meta-group operations, list tuples
The list is one of the most common data types in the future. You can use the list to conveniently store and modify data.
Definition list:
| 1 |
list = ['1','2','3','4','5','6'] |
Access the elements in the list by subscript. The subscript starts counting from 0.
Slice: Multiple Elements
>>> List = ['1', '2', '3', '4', '5 ', '6'] >>> list [] # obtain numbers between subscript 1 and subscript 4, including 1, excluding 4 ['2', '3 ', '4'] >>> list [1:-1] # obtain the values from subscript 1 to-1, excluding-1 ['2', '3 ', '4', '5'] >>> list [0: 3] ['1', '2', '3'] >>> list [: 3] # If the value starts from 0, 0 can be ignored. The result is the same as that in the sentence ['1', '2', '3'] >>> list [3:] # If you want to get the last one, you cannot write-1. You can only write ['4', '5', '6'] >>> list [3: -1] # in this way,-1 will not be included in ['4', '5'] >>> list [0: 2] # The following 2 represents: take a ['1', '3', '5'] >>> list [: 2] for each element. # the same effect as the preceding clause ['1 ', '3', '5']
Slice
Append
>>> list['1', '2', '3', '4', '5', '6']>>> list.append("a")>>> list['1', '2', '3', '4', '5', '6', 'a']Append
Insert
>>> List ['1', '2', '3', '4', '5', '6'] >>> list. append ("a") >>> list ['1', '2', '3', '4', '5', '6 ', 'A'] >>> list. insert (2, "B") # insert B> list ['1', '2', 'B', '3', '4' from the front of subscript 2 ', '5', '6', 'a'] >>> list. insert (5, "c") # insert c into the subscript 5> list ['1', '2', 'B', '3', '4 ', 'C', '5', '6', 'a']Insert
Modify
>>> list['1', '2', 'b', '3', '4', 'c', '5', '6', 'a']>>> list[2] = "B">>> list['1', '2', 'B', '3', '4', 'c', '5', '6', 'a']
Modify
Delete
>>> List ['1', '2', 'B', '3', '4', 'C', '5', '6 ', 'A'] >>> del list [2] >>> list ['1', '2', '3', '4', 'C ', '5', '6', 'a'] >>> del list [4] # Delete the data of subscript 4 >>> list ['1', '2 ', '3', '4', '5', '6', 'a'] >>> list. remove ("6") # delete a specified Element >>> list ['1', '2', '3', '4', '5 ', 'A'] >>> list. pop () # Delete the last list value 'a'> list ['1', '2', '3', '4', '5']Delete
Extension
>>> list['1', '2', '3', '4', '5']>>> list_b = ['a','b','c']>>> list.extend(list_b)>>> list['1', '2', '3', '4', '5', 'a', 'b', 'c']
Extension
Copy
>>> list['1', '2', '3', '4', '5', 'a', 'b', 'c']>>> list_copy = list.copy()>>> list_copy['1', '2', '3', '4', '5', 'a', 'b', 'c']
Copy
Statistics
>>> list['1', '2', '3', '4', '5', 'a', 'b', 'c', '1']>>> list.count('1')2Statistics
Sort & flip
>>> List = ['1', 'A', '2', 'C', 'B', '4', '3 ', '5'] >>> list ['1', 'A', '2', 'C', 'B', '4', '3 ', '5'] >>> list. sort () # sort> list ['1', '2', '3', '4', '5', 'A', 'B ', 'C'] >>> list. reverse () # reverse> list ['C', 'B', 'A', '5', '4', '3', '2 ', '1']
Sort & Reverse
Obtain subscript
>>> list['c', 'b', 'a', '5', '4', '3', '2', '1']>>> list.index('a')2Obtain subscript
Tuples
It is also called the read-only list, because it is similar to the list, but once created, it cannot be changed
Definition:
>>> Tuple = (1, 2, 3, 3, 4, 4) >>> tuple. count (3) 3 >>> tuple. index (4) # Same as the list, only the subscript 6 of the first element is returned.
Tuples