Python list operation:
- The sequence is the most basic data structure in Python. Each element in the sequence is assigned a number-its position, or index, the first index is 0, the second index is 1, and so on.
- Python has 6 built-in types of sequences, but the most common are lists and tuples.
- Sequences can be performed by operations including indexing, slicing, adding, multiplying, and checking members.
- In addition, Python has built-in methods for determining the length of a sequence and determining the maximum and minimum elements.
- A list is the most commonly used Python data type and can appear as a comma-separated value within a square bracket.
- Data items for a list do not need to have the same type
List creation and update:
1 #List Updates2List = ['Physics','Chemistry', 1997,2000]3 Print('Value available at index 2:', List[2])#the index of the list starts at 0, followed by 14LIST[2] = 2001#the list can be updated by direct assignment5 Print('Value available at index 2:', list[2])6 7 #Results:8 #Value available at index 2:19979 #Value available at index 2:2001
Add elements to the list:
1 #adding elements to a list using append2List = ['Physics','Chemistry']3List.append ('Wangtao')#add element ' Wangtao ' to the list4 Print(list[1])5 Print(list)6 7 #Results8 #Chemistry9 #[' Physics ', ' chemistry ', ' Wangtao ']
To delete a list element:
1 #To delete a list element2List = ['Physics','Chemistry', 1997, 2000]3 Print(list)4 delLIST[1]#use the Del statement to delete a list element5 Print(list)6 7 #Results8 #[' Physics ', ' Chemistry ', 1997, +]9 #[' Physics ', 1997, +]
List operators:
1 #List Operators2 Importoperator3List = [1,2,3,4,5]4List2 = [2,3,4,5]5 Print(Len (list))#Len is used to get the length of the list6 Print(LIST+LIST2)#' + ' combines two elements of a list into a single list7 Print(list*2)#' * ' repeating list8 Print(3inchList#determines whether an element is in the list9 Print(6inchList#determines whether an element is in the listTen Print(max (list))#returns the maximum value of an element in a list One Print(min (list))#returns the minimum value of an element in a list A Print(Operator.eq (LIST,LIST2))
1 #some operator operations in Python2 operator.lt (A, b)3 Operator.le (A, b)4 Operator.eq (A, b)5 Operator.ne (A, b)6 operator.ge (A, b)7 operator.gt (A, b)8operator.__lt__(A, b)9operator.__le__(A, b)Tenoperator.__eq__(A, b) Oneoperator.__ne__(A, b) Aoperator.__ge__(A, b) -operator.__gt__(A, B)
- #There is no CMP function in Python 3, you need to call the operator module if you need to compare the elements of the two list - the - #Results: - #5 - #[1, 2, 3, 4, 5, 2, 3, 4, 5] + #[1, 2, 3, 4, 5, 1, 2, 3, 4, 5] - #True + #False A #5 at #1 - #True
List interception:
1 #List Interception2List = ['Physics','Chemistry', 1997, 2000]3 Print(List[2])#an element with an output index of ' 2 '4 Print(List[-3])#The third element to the bottom of the output5 Print(list[1:])#output list elements starting from index ' 1 '6 7 #Results:8 #19979 #ChemistryTen #[' Chemistry ', 1997, +]
Remaining list operations:
1. List.append (obj): Add new Object 2, List.count (obj) at the end of the list: count the number of occurrences of an element in the list 3, list.extend (seq) : Append multiple values from another sequence at the end of the list (with a new list) 4, List.index (obj): Find a value from the list index position of the first occurrence 5, List.insert (index, obj): Inserts an object into 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): Removes the first occurrence of a value in the list 8, List.reverse () : Reverse list of elements 9, List.sort ([func]): Sort the original list
Python Learning-list Operations