A python data structure is a collection of data elements that are organized together in a certain way (such as numbering elements). These data elements can be numbers or characters, or even other data structures. In python, the most basic data structure is sequence ). Each element in the sequence is assigned a sequence number, that is, the position of the element, which is also indexed. Sequence overview python contains six built-in sequence examples: [python] edward = ['edward gumby', 42] all elements in the index sequence are numbered-increasing from 0. [Python] greeting = 'hello' greeting [0] Output: h fragment is similar to accessing a single element using an index. You can use a fragment operation to access a certain range of elements. Clustering is achieved through two indexes separated by colons: [python] tag = '<a href = "http://www.python.org"> Python web sit </a>' tag [] tag [32: -4] basic list operations 1. Change the list: Element assignment [python] x = [, 1] x [1] = 2 x output, 1] 2. Delete the element [python] names = ['Alice ', 'beth', 'cecil ', 'dee-dee'] del names [2] names output: ['Alice ', 'beth', 'dee-dee'] 3. assign a partition value to [python] name = list ('perl ') name [2:] = list ('ar ') name output: ['P', 'E', 'A ', 'R'] The partition assignment statement can insert a new element without replacing any original element. [python] Numbers = [] numbers [] = [, 4] numbers output: [, 5] The list method is a function closely related to some objects, objects may be lists, numbers, strings, or other types of objects. In general, a method can be called like this: except that an object is placed before the method name, and the two are separated by a vertex. method calls are similar to function calls. 1. append a new object. The append method is used to append a new object to the end of the list: [python] lst = [1, 2, 3] lst. append (4) lst output: [1, 2, 3, 4] 2. count the number of times an element appears. count the number of times an element appears in the list. [python] ['to' be ''or ''not'''' be ']. count ('to') 3. The one-time append extend method at the end of the extend list can append multiple values in another sequence at the end of the list. (You can use the new list to extend the original list.) [python] a = [, 3] B = [, 6]. extend (B) a output: [1, 2, 3, 4, 5, 6] Use multipart assignment to achieve the same result: [python] a = [1, 2, 3] B = [4, 5, 6] a [len (a):] = B a: The output result is the same. 4. The index position index method is used to locate the index position of the first matching item of a value in the list. [python] knights = ['I', 'are ', the ', 'knights', 'who ', 'say', 'ni'] knights. index ('who ') 5. insert method is used to insert objects into the list [python] numbers = [, 7] numbers. insert (3, 'four ') Output: [1, 2, 3, 'four', 5, 6, 7] Same as the extend method, Insert operations can also be performed using multipart assignment. 6. The pop removal method removes an element from the list (the last one by default ), and returns the value of this element [python] x = [, 3] x. pop () 3 x [1, 2] 7. remove method is used to remove the first matching item of a value in the list [python] x = ['to', 'be ', 'or', 'not ', 'to', 'be'] x. remove ('be') 8. The reverse method stores the elements in the list in reverse direction. 9. The sort method is used to sort the list in the original position. Sorting by the original position means changing the list of the original list so that the elements can be sorted in a certain order, instead of simply returning a sorted list copy [python] x = [,] x. sort () x output: [1, 2, 4, 6, 7, 9] ============================ [python] x =, 7, 9] y = x [:] y. the sort () y output is the same as the preceding result and calls x [:] again. the result is that all parts of X are ============================ another method to obtain a list copy that has been sorted. the method is, use the sorted function [python] x = [,] y = sorted (x .) 10. For advanced sorting, if you want the elements to be sorted in a specific way (instead of the default method of the sort function, that is, sort the elements in ascending order according to the default sorting rules of python ), you can use compare (x, y) to customize the comparison function. The compare (x, y) function returns a negative number when x <y, returns a positive number when x> y, and returns 0 if x = y. After defining the function, you can provide the sort method as a parameter. The built-in function cmp provides the default Implementation Method for comparison functions: [python] cmp (99,100) Output: 1 cmp () Output:-1 cmp () output: 0 numbers = [5, 2, 9, 7] numbers. the sort (cmp) numbers [,] sort method has two other optional parameters: key and reverse. If you want to use them, you must specify [python] x = ['mongodvark', 'abalone', 'acme ', 'add', 'aerate'] x by name. sort (key = len) x output: ['add', 'acme ', 'aerate', 'abalone ', 'reverse dvark'] Another keyword parameter reverse is a simple boolean value (True or False) used to indicate whether the list needs to be sorted in reverse order [python] x = [,] x. sort (reverse = True) x output: The parameters [9, 7, 6, 4,] cmp, key, and reverse can all be used in sorted functions. In most cases, it is very useful to provide custom functions for cmp or key: immutable sequence tuples, like lists, are also a sequence. The only difference is that the tuples cannot be modified. The syntax for creating tuples is simple: if you use commas to separate some values, you will automatically create tuples. [Python], 3 (, 3) tuples are enclosed by parentheses: [python] (, 3) empty tuples can be expressed using two parentheses that do not contain content: [python] (). How to Implement tuples that contain a value. Implementation Method --- price comma is required, even if there is only one value [python] 42, [python] 3*(40 + 2,) Output: (, 42) 1. The functions of the tuple function are basically the same as those of the list function: Use a sequence as a reference and convert it into a tuples. If the parameter is a tuple, this parameter will be returned as is [python] tuple ([, 3]) Output :( 1, 2, 3) tuple ('abc ') output: ('A', 'B', 'C') tuple (, 3) Output: (, 3) New Function cmp (x, y) compare two values of len (seq) returned sequence length list (seq) to convert the sequence to list max (args) returned sequence or the maximum min (args) in the parameter set) returns the minimum value reversed (seq) in a sequence or a set of parameters to perform reverse iteration sorted (seq) on the sequence. returns the list of sorted all elements including seq. tuple (seq) converts the sequence to a metagroup.