Python list and metadata group, python list
1. List
1.1 Basic operations on the list:
Index, slice, append, delete, length, slice, cycle, and contain
>>> Name_list = ['Alex ', '65brother', 'tenglanc'] # initialize a list >>> type (name_list) # determine the type <type 'LIST' >>> help (name_list) # obtain detailed help information >>> dir (name_list) # obtain simple information [..... omitted, 'append', 'Count', 'extend', 'index', 'insert', 'pop', 'delete', 'reverse ', 'sort '] >>> name_list.append ("xiaolan") # append a value >>>> name_list # view the append result ['Alex', '65brother ', 'tenglan ', 'xiaolanc'] >>> name_list.insert (0, "xiaolan") # Append content using the index value >>>> name_list # view the append result ['xiaolanc', 'Alex ', '65brother ', 'tenglanc', 'xiaolanc']> name_list.count ("xiaolan") # calculate the total number of "xiaolei"> name_list.index ("65 brother ") # view a value worth indexing >>> name_list [2] # Use '65brother '> name_list.remove ("alex ") # Remove the first scanned alex value >>> name_list ['xiaolanc', '65brother ', 'tenglanc', 'xiaolanc'] # view the removal result >>> name_list.pop () # remove the last value and return the value 'xiaolanc'> name_list # view the removal result ['xiaolanc', '65brother ', 'tenglanc']View Code
1.2 extend)
>>> A [1, 2, 3, 4, 6, 'A', 'B'] >>> B = ['C', 'D', 4,24, 6] >>> a + B # merge two lists: [1, 2, 3, 4, 6, 'A', 'B', 'C', 'D ', 4, 24, 6] >>>. extend (B) # extend B's content to a list> a [1, 2, 3, 4, 6, 'A', 'B', 'C ', 'D', 4, 24, 6] >>> name = "Alex Li" >>>. extend (name) # split each letter of the string to list a> a [1, 2, 3, 4, 6, 'A', 'B ', 'C', 'D', 4, 24, 6, 'A', 'l', 'E', 'x', '', 'l ', 'I']
1.3 inclusion
>>> A [1, 2, 3, 4, 6, 'A', 'B', 'C', 'D', 4, 24, 6, 'A', 'l', 'E', 'x', '','', 'l ', 'I'] >>> "A" in a # whether the letter A is included in list a True >>> 24 in a # Whether the number 24 is included in list a True
1.4 list application scenarios
Case 01: 10 million data records and 100 million duplicate data records must be deleted.
>>> Name_list ['Alex ', '65brother', 'tenglanc', 'lanyulei ', 'lanlilei'] >>> for I in range (name_list.count ('lanlilei ')):... name_list.remove ('lanyulei')...> name_list ['Alex ', '65brother', 'tenglanc']View Code
1.5 slice
>>> A = [1, 2, 3, 'A', 'B'] >>> a [0: 2] # starting from the index value 0, end with the value of 2, excluding the value of [1, 2]> a [] # starting from the value of 1, end with the value of 2, excluding the value of 2 [2, 3, 'a'] >>> a [0: 4] [1, 2, 3, 'A'] >>> a [] # starts from the value of index 0 and ends with the value of index 4, excluding the value of index 4, then, leave a value [1, 3] >>> a [] # starting from the value of 0 to the value of 4, does not include the value of 4 in the index, and then leaves a value of [1, 'a'] >>> a = [, 3, 'A' at intervals of three ', 'B'] >>>> a [-1] # Take the first reciprocal value 'B' >>> a [-2:-1] # Take the two reciprocal values, then take the first reciprocal value ['a'] >>> a [-2:] # Take the two reciprocal values ['A ', 'B']> a [: 3] # obtain the first three values [1, 6, 2] ''' in Python 3. in array x, the sort function cannot be used if there are numbers and strings in the list sort. If you need to sort the list, you must first implement slicing and then perform partitioning '''c: \ Users \ Administrator> python3Python 3.5.0 (v3.5.0: 374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright ", "credits" or "license" for more information. >>> a = [1, 6, 2, 4, 3, 'A', 'B']> B =. sort () Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str () <int () C: \ Users \ Administrator> python3Python 3.5.0 (v3.5.0: 374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32Type "help", "copyright ", "credits" or "license" for more information. >>> a = [1, 6, 2, 4, 3, 'A', 'B']> B =. sort () Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str () <int () # First slice and then sort> a = [, 3, 'A', 'B']> B = a [0: 5]> B. sort () >>> B [1, 2, 3, 4, 6]View Code
1.6
Sort Python3.x
>>> A = [1, 5, 2, 3, 'A', 'B'] >>>. sort () Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str () <int ()View Code
2. tuples
The functions of tuples can be implemented in the list of functions that can be implemented by tuples. Most tuples used in work are used to remind others that this is a tuple and cannot be changed.
>>> T = (1, 2, 3, 4, 'A', 'LIST') >>> type (t) <class 'tuple' >>> dir (t) [...... omitted, 'Count', 'index']
Conversion list of tuples (modifying tuples)
Tuples --> List
>>> T (1, 2, 3, 4, 'A', 'LIST') >>> list (t) [1, 2, 3, 4, 'A', 'LIST']
List --> tuples
>>>
[1, 2, 3, 4, 6, 'A', 'B', 'C', 'D', 4, 24, 6, 'A ', 'l', 'E', 'x', '', 'l', 'I']
>>> Tuple ()
(1, 2, 3, 4, 6, 'A', 'B', 'C', 'D', 4, 24, 6, 'A ', 'l', 'E', 'x', '', 'l', 'I ')