Python learning notes (8) Python list (2)

Keywords: Python

List function

Append and expand

list.append() appends a new object to the end of the list

 1 >>> dir(list)  #Dirfunctions for viewing lists
 2 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
 3 >>> help(list.append) #help to view the details of the list.append() function
 4 Help on method_descriptor:
 5 
 6 append(...)
 7     L.append(object) -- append object to end  #Append object to end
 8 
 9 >>> a =[2,6]
10 >>> a.append(50)  #Append object 50 to list a
11 >>> a
12 [2, 6, 50]
13 >>> a.append("python book") 
14 >>> a
15 [2, 6, 50, 'python book']
16 >>> a
17 [2, 6, 50, 'python book', ['baidu', 'weibo']]
18 >>> b =[1]
19 >>> id(b)   #The id return value gives the space in memory
20 60126664L
21 >>> b.append(5)
22 >>> b
23 [1, 5]
24 >>> b
25 [1, 5]
26 >>> id(b) #After adding 5, the return value of id is the same
27 60126664L
28 >>> b.append("zhangsan")
29 >>> id(b)
30 60126664L
31 >>> b
32 [1, 5, 'zhangsan']
33 >>>
Note: when the list is modified, instead of creating a new list, the original list is modified. This modification is called in place modification

extend() appends multiple values from another sequence at the end of the list at once (extending the original list with a new list)

 1 >>> help(list.extend) #help to see the details of the list.extend function
 2 Help on method_descriptor:
 3 
 4 extend(...)
 5     L.extend(iterable) -- extend list by appending elements from the iterable  #Append elements from iteratable objects to the list
 6 
 7 >>> a=[1,2,3]  #Iteratable object
 8 >>> b=[4,5,6]  #Iteratable object
 9 >>> a.extend(b) 
10 >>> a           #The elements in the b list are appended to the a list one by one
11 [1, 2, 3, 4, 5, 6] 
12 >>> a.extend("python")
13 >>> a
14 [1, 2, 3, 4, 5, 6, 'p', 'y', 't', 'h', 'o', 'n']
15 >>> alst =[1,2]20 >>> hasattr(alst,'__iter__') #Judge whether it can be iterated, return true, otherwise false
21 True
22 >>> hasattr("python",'__iter__') #The string cannot be iterated. Here is to tear out and append one character of the string
23 False
24 >>> a =[1,2]
25 >>> a.append([4,5])
26 >>> a
27 [1, 2, [4, 5]]
28 >>> a.extend([4,5]) 
29 >>> a
30 [1, 2, [4, 5], 4, 5]
31 >>> b =[9,8]
32 >>> a.append(b[0])
33 >>> a.append(b[1])
34 >>> a
35 [1, 2, [4, 5], 4, 5, 9, 8]
36 >>>
Note: append And extend The difference is, extend Split and append the elements one by one, append It is an overall addition

Other functions

count() counts the number of times an element appears in the list

 1 >>> help(list.count)
 2 Help on method_descriptor:
 3 
 4 count(...)
 5     L.count(value) -> integer -- return number of occurrences of value  #Returns the number of occurrences
 6 
 7 >>> a =[1,1,1,2,2,2,3,3,3]
 8 >>> a.count(1)   #1 this element appears three times in the list
 9 3
10 >>> a.count(2)  #2 this element appears three times in the list
11 3
12 >>> a.count("a") #a is not in the list
13 0

index() finds the index position of the first match of a value from the list

 1 >>> help(list.index)
 2 Help on method_descriptor:
 3 
 4 index(...)
 5     L.index(value, [start, [stop]]) -> integer -- return first index of value. #Index location where the value value in the parameter first appears in the list
 6     Raises ValueError if the value is not present.
 7 
 8 >>> a.index(1)
 9 0
10 >>> a.index(2)
11 3
12 >>>

insert() inserts objects into the list

 1 >>> help(list.insert)
 2 Help on method_descriptor:
 3 
 4 insert(...)
 5     L.insert(index, object) -- insert object before index  #Insert the object in front of the element corresponding to the index
 6 
 7 >>> a =["python","web"]
 8 >>> a.insert(1,"aaa")   #Insert a string aa before the element with index 1
 9 >>> a
10 ['python', 'aaa', 'web']
11 >>> a.insert(0,"like") #Insert a string like at the top
12 >>> a
13 ['like', 'python', 'aaa', 'web']
14 >>>
Note: insert Modified in place, no new list created

pop() removes an element from the list (the last element by default) and returns the value of that element

 1 >>> help(list.pop)
 2 Help on method_descriptor:
 3 
 4 pop(...)
 5     L.pop([index]) -> item -- remove and return item at index (default last).  #Delete the element corresponding to the index and return it as the return value (delete the last element by default)
 6     Raises IndexError if list is empty or index is out of range. #Cannot delete empty or out of index elements, otherwise index error
 7 
 8 >>> a
 9 ['like', 'python', 'aaa', 'web']
10 >>> a.pop(1)
11 'python'
12 >>> a
13 ['like', 'aaa', 'web']
14 >>>

remove() removes the first match for a value in the list

 1 >>> help(list.remove)
 2 Help on method_descriptor:
 3 
 4 remove(...)
 5     L.remove(value) -- remove first occurrence of value.  #Remove a match worth the first
 6     Raises ValueError if the value is not present.
 7 
 8 >>> a =["test","test","demo"]
 9 >>> a.remove("test")
10 >>> a
11 ['test', 'demo']
12 >>> a.remove("aa")
13 Traceback (most recent call last):
14   File "<stdin>", line 1, in <module>
15 ValueError: list.remove(x): x not in list
16 >>>

reverse() elements in reverse list

 1 >>> help(list.reverse)
 2 Help on method_descriptor:
 3 
 4 reverse(...)
 5     L.reverse() -- reverse *IN PLACE*
 6 
 7 >>> a =[1,2,3,4,5]
 8 >>> a.reverse()
 9 >>> a
10 [5, 4, 3, 2, 1]
11 >>>

sort() sorts the original list

 1 >>> help(list.sort)
 2 Help on method_descriptor:
 3 
 4 sort(...)
 5     L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
 6     cmp(x, y) -> -1, 0, 1
 7 
 8 >>> a =[5,9,3,1]
 9 >>> a.sort()  #Arrange from small to large
10 >>> a
11 [1, 3, 5, 9]
12 >>> b=[9,3,8,6]
13 >>> b.sort(reverse=True) #From large to small
14 >>> b
15 [9, 8, 6, 3]
16 >>>

Posted by TheWart on Tue, 31 Mar 2020 13:48:32 -0700