List common actions
Slices: Fetching multiple elements
>>> names = [' Amy ', ' Bob ', ' Cindy ', ' David ']>>> names[1:4][' Bob ', ' Cindy ', ' David ']>>> names[ -1] ' David ' >>> names[:-1][' Amy ', ' Bob ', ' Cindy ']>>> names[::2][' Amy ', ' Cindy ']>>> names[:3 [' Amy ', ' Bob ', ' Cindy ']>>> names[1:][' Bob ', ' Cindy ', ' David ']
Additional
>>> names = [' Amy ', ' Bob ', ' Cindy ', ' David ']>>> names. Append (' Eric ') >>> names[' Amy ', ' Bob ', ' Cindy ', ' David ', ' Eric '
Insert
>>> names[' Amy ', ' Bob ', ' Cindy ', ' David ', ' Eric ']>>> names. Insert (2, ' Insert from behind Bob ') >>> names[' Amy ', ' Bob ', ' Insert from behind Bob ', ' Cindy ', ' David ', ' Eric ']
Modify
>>> names[' Amy ', ' Bob ', ' Insert from behind Bob ', ' Cindy ', ' David ', ' Eric ']>>> names[2] = ' Baby ' >>> names[' Amy ', ' Bob ', ' Baby ', ' Cindy ', ' David ', ' Eric '
Delete
>>> names[' Amy ', ' Bob ', ' Baby ', ' Cindy ', ' David ', ' Eric ']>>> del names[2]>>> names[' Amy ', ' Bob ', ' Cindy ', ' David ', ' Eric ']>>> names. Remove (' David ') >>> names[' Amy ', ' Bob ', ' Cindy ', ' Eric ']>>> names. Pop () ' Eric ' >>> names[' Amy ', ' Bob ', ' Cindy '
Extended
>>> names[' Amy ', ' Bob ', ' Cindy ']>>> age = [18,28,38]>>> names. Extend (age) >>> names[' Amy ', ' Bob ', ' Cindy ', 18, 28, 38]
Copy
>>> names[' Amy ', ' Bob ', ' Cindy ', 38]>>> names_copy = names. Copy () >>> names_copy[' Amy ', ' Bob ', ' Cindy ', +, 38]>>> names.remove ($) >>> names[' Amy ', ' Bob ', ' Cindy ', 28]>>> names_copy[' Amy ', ' Bob ', ' Cindy ', 18, 28, 38]
Statistics
>>> names.append >>> names[' Amy ', ' Bob ', ' Cindy ',,, and 28]>>> names. Count (28) 2
Sort & Flip
>>> Names.sort () Traceback (most recent): File "<stdin>", line 1, in <module>typeerror : Unorderable types:int () < Str () #Python3.0 Different data types cannot be sorted together >>> names[3] = ' + ' >>> names[4] = ' 28 ' >>> ' Amy ', ' Bob ', ' Cindy ', ' names[', ']>>> ' names. Sort () >>> names[' + ', ' I ', ' Amy ', ' Bob ', ' Cindy ']>>> names[', ' I ', ' Amy ', ' Bob ', ' Cindy ']>>> Names. Reverse () >>> names[' Cindy ', ' Bob ', ' Amy ', ' 28 ', ' 18 '
Get subscript
>>> names[' Cindy ', ' Bob ', ' Amy ', ' ['] ', ' ' + ', ']>>> ' names. Index (' 3 ') #只返回找到的第一下标
List-related functions
Serial Number |
function |
1 |
Len (list) Number of list elements |
2 |
Max (list) Returns the maximum value of a list element |
3 |
MIN (list) Returns the minimum value of a list element |
4 |
List (seq) Convert a tuple to a list |
List of built-in methods
Serial Number |
Method |
1 |
List.append (obj) Add a new object at the end of the list |
2 |
List.count (obj) Count the number of occurrences of an element in a list |
3 |
List.extend (seq) Append multiple values from another sequence at the end of the list (extend the original list with a new list) |
4 |
List.index (obj) Find the index position of the first occurrence of a value from the list |
5 |
List.insert (index, obj) Inserting an object into a list |
6 |
List.pop (Obj=list[-1]) Removes an element from the list (the last element by default), and returns the value of the element |
7 |
List.remove (obj) To remove the first occurrence of a value in a list |
8 |
List.reverse () Reverse List of elements |
9 |
List.sort ([func]) Sort the original list |
10 |
List.clear () Clear List |
11 |
List.copy () Copy List |
Python Basics 5-List