Source: AI introductory learning
List is a very important data type in python. There are 11 methods in total. If you master these methods, the efficiency of data processing will be greatly improved. Now I will share with you.
print(dir(list()))#Method of viewing the list [ ..., 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
01, append() method
Description: The append() method adds an element object at the end (end) of the list ls
Syntax: ls.append(object) object is the element to be added.
Parameters: object can add lists, dictionaries, tuples, sets, strings, etc.
The operation object of the #append() function is the original list. ls = [1,2,3,4,5,6] ls.append(12)#Add element print (ls) [1, 2, 3, 4, 5, 6, 12] ls.append([1,"a"]) #Add list print (ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a']] ls.append({2:"a",3:"hj"}) #Add dictionary print (ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}] ls.append((1,"k",3)) #add tuple print (ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3)] ls.append({"1","2","h"}) #添加集合 print (ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3), {'2', 'h', '1'}] ls.append("123abc") #Add string print (ls) [1, 2, 3, 4, 5, 6, 12, [1, 'a'], {2: 'a', 3: 'hj'}, (1, 'k', 3), {'2', 'h', '1'}, '123ab
02, clear() method
Description: Delete all elements in the list ls.
Syntax: ls.clear()
ls = [1,2,3,"4",5,"a"] ls.clear() print (ls) []
03, copy() method
Description: Generate a new list and copy all elements in ls.
Syntax: ls.copy() -> list returns a list
ls = [1,2,3, [4,5,6]] lt = ls.copy() #lt copy all elements in ls ls.clear() #Delete all elements of ls, the elements in lt have not been deleted. lk = ls #This is not a copy, but a new reference to the list ls, that is, add an alias, ls and lt point to the same memory address. print(id(ls),id(lk)) print(lt) print (ls)
04, count() method
Description: Count the number of occurrences of the value element in the list
Syntax: ls.count(value) -> integer returns an integer
Parameters: value--value element to be counted.
ls = [1,2,3,5,4,5,5,5,5,"python"] ls.count(5) #Count the number of occurrences of 5 in ls 5 ls.count(0)#There is no 0 element in the list ls 0 ls.count("python") #Count the number of occurrences of "python" in the list ls.
05, extend() method
Description: Add a list iterable at the end of the list ls.
Syntax: ls.extend(iterable) -> None No return value
Parameters: iterable - the list to be added. It can be the entire list iterable or part of the list iterable.
Note: The difference between extend() and append() is that extend() does not treat the list or the ancestor as a whole, but adds the elements they contain to the list one by one
ls = [1,2,"a",[4,5,"a"]] lt = [1,"abc","b",[1,2]] ls.extend(lt) #The return value is empty, and the elements of the list lt are added to the end of the list ls. print(ls.extend(lt)) None print (ls) [1, 2, 'a', [4, 5, 'a'], 1, 'abc', 'b', [1, 2], 1, 'abc', 'b', [1, 2]] print(lt) #list lt element unchanged [1, 'abc', 'b', [1, 2]]
06, index() method
Description: The position where the element value first appears in the list ls.
Syntax: ls.index(value, start, stop) -> integer returns an integer
parameter:
- value —— The element to find.
- star-the starting position of the index.
- stop-the end position of the index.
ls = [1,2,3,"a",3,5,"a",5,[1,7,"b"]] ls.index("a") #Returns the position of the first occurrence of "a" in the list ls. 2 ls.index("a",4) #The starting position of the index is the element with subscript 4, and the index range is 3, 5,'a', 5, [1, 7,'b' 6 ls.index("a",4,8) #The starting position of the index is the element with subscript 4, and the ending position is the element with subscript 7. Index range 3, 5,'a', 5 6
07, insert() method
Description: Add the element object at the index position of the list.
Syntax: ls.insert(index, object)
index —— The position where the element object is inserted into the list ls.
objece-the element to be added. It can be a list, tuple, dictionary, set, string, etc.
ls = [1,2,"a",["a",5,8]] ls.insert(3,"b")#Insert the element "b" at the position marked 3 in the list ls print (ls) [1, 2, 'a', 'b', 'b', ['a', 5, 8]] ls.insert(3,1) #Insert the element at the position marked 3 in the list ls 1 print (ls) [1, 2, 'a', 1, 'b', 'b', ['a', 5, 8]] ls.insert(1,['a', 5, 8]) #Nesting the list within the list ls["a",5,8] print (ls) [1, ['a', 5, 8], 2, 'a', 1, 'b', 'b', ['a', 5, 8]] ls = [1,2,3] ls.insert(0,[1,2,3]) #insert list ls.insert(0,(1,2,3)) #insert tuple ls.insert(0,{1:"a",2:"b"}) #insert dictionary ls.insert(0,{1,2,3}) #insert collection print (ls) [{1, 2, 3}, {1: 'a', 2: 'b'}, (1, 2, 3), [1, 2, 3], 1, 2
08, pop() method
Description: Take the index item element out of the list ls, and delete the element from the list ls. If index is omitted, the last (end) element of the list will be deleted by default and the element will be returned.
Syntax: ls.pop(index) -> item returns the deleted item
Parameters: index - the ordinal number of the element index to be retrieved and deleted.
ls = [1,2,"a","y",[1,2,3],"b"] ls.pop(0)#Remove the element whose subscript is 0 and delete it from the list ls. 1 print (ls) [2, 'a', 'y', [1, 2, 3], 'b'] ls.pop() #By default, the last element of the list ls is taken out and deleted. 'b' print (ls) [2, 'a', 'y', [1, 2, 3]
09, remove() method
Description: Delete the first element value that appears in the list ls.
Syntax: ls.remove(value) -> None The return value is empty
Parameters: value - the element to be deleted.
ls1 = [1,2,"a",3,1,1,55,"a,1"] ls2 = [1,2,"a",3,1,1,55,"a,1"] ls1.remove(1) #Remove the first occurrence of the element in ls1 1 ls2.remove("a") ##Remove the first occurrence of element "a" in ls2 print(ls1.remove(1)) #The return value is empty print(ls1) print(ls2)
10. The reverse() method
Description: Reverse the elements in the list ls.
Syntax: ls.reverse()
ls1 = [1,2,3,4,5,6,7,8,9] ls1.reverse() #Reverse output of list ls1 print(ls1) [9, 8, 7, 6, 5, 4, 3, 2, 1] ls2 = [2,5,8,9,4,1,2,6,2,1,3] ls2.sort(reverse=True) #ls2.sort() arranges the list ls2 in ascending order by default. reverse=True reverses the sorted list, reverse=False does not reverse print(ls2) [9, 8, 6, 5, 4, 3, 2, 2, 2, 1, 1]
11. The sort() method
Description: Sorting the elements in the original list ls means changing the original list instead of returning a list
Syntax: ls .sort([key=None][,reverse=False])--No return value, but the elements in the list will be sorted.
parameter:
- key-- Optional parameter, if this parameter is specified, the method of this parameter will be used for sorting.
- reverse-- Optional parameter, whether to reverse the order, the default is False.
ls = [1,3,7,2,4,5,6] ls.sort () print (ls) [1, 2, 3, 4, 5, 6, 7]#The original list has changed What to do when the user needs an arranged list while keeping the original list Error method 1: ls = [1,3,7,2,4,5,6] y = ls.sort () print (and) None print (ls) #Error method 2: ls = [1,3,7,2,4,5,6] y = lsy.sort () print (ls) [1, 2, 3, 4, 5, 6, 7] print (and) [1, 2, 3, 4, 5, 6, 7] The correct way: ls = [1,3,7,2,4,5,6] y = ls [:] y.sort () print (ls) [1, 3, 7, 2, 4, 5, 6]#The old list has not changed print(y)[1, 2, 3, 4, 5, 6, 7]#New list changes Another way to get a copy is to use the sorted() function ls = [1,3,7,2,4,5,6] y = sorted (ls) print (and) [1, 2, 3, 4, 5, 6, 7] print (ls) [1, 3, 7, 2, 4, 5, 6]#No change Descending method, use sort or sorted() and then reverse() to reverse ls = [1,3,7,2,4,5,6] y = sorted (ls) y.reverse () print (and) [7, 6, 5, 4, 3, 2, 1] Of course, the sort method also has two parameters, key and reverse ls = ['a22112x', 'aaaba', 'xxvvv', '5aa', 'wodesddddssd'] ls.sort(key=len) print (ls) ['5aa', 'aaaba', 'xxvvv', 'a22112x', 'wodesddddssd'] ## Sort by the number of a Define a function to calculate the number of'a' ls = ['1a22112x', '2aaaba', '3xxvvv', '4b5aa', '5wodesddddssd'] def a_fun(x): return str(x).count('a') ls.sort(key=a_fun) print (ls) ['3xxvvv', '5wodesddddssd', '1a22112x', '4b5aa', '2aaaba'] ls = [1,3,7,2,4,5,6] ls.sort(reverse=True) [7, 6, 5, 4, 3, 2, 1]