"The Road to Python Learning - The Method of Lists"

  As mentioned earlier, a list is an ordered collection used to store data of various data types. The data stored in a list is also called an element. The elements are separated by commas; Index [ ] to access the elements inside. In addition to that, this article will cover other ways to manipulate lists:

list.append(p_object)

This method is used to add an element p_object to the end of the list, this method changes the list and returns the updated list

num_list = [1,2,3,4]
num_list.append( 5 )
 print (num_list) #Output   : [1, 2, 3, 4, 5]

 

 

list.clear( )

This method is used to empty the list and returns an empty list

num_list = [1,2,3,4]
num_list.clear()
print (num_list) #output   : []

 

 

list.copy( )

This method is used to copy (shallow) a list and returns a copy of the list

num_list = [1,2,3,4]
new_list = num_list.copy()
print(new_list,id(new_list) == id(num_list))  # 输出:[1, 2, 3, 4] False

 

 

list.count(value)

This method is used to count the number of times the value value appears in the list, and the result returns this number of times

num_list = [1,2,3,4,2,3,5,2]
num = num_list.count(2 )
 print (num)   #output : 3

 

 

list.extend(iterable)

This method is used to expand the list. The parameter iterable must be an iterable (traversable) object, such as: list, string, etc. This method will change the original list, and the result will return the updated list

num_list = [1,2,3,4]
my_str = 'hello jonas'
num_list.extend(my_str)
print (num_list) #Output   : [1, 2, 3, 4, 'h', 'e', ​​'l', 'l', 'o', ' ', 'j', 'o', 'n' , 'a', 's'] 
num2_list = [1,2,3,4 ]
num3_list = [5,6,7]
num2_list.extend(num3_list)
print (num2_list) #Output   : [1, 2, 3, 4, 5, 6, 7]

 

 

list.insert(index,p_object)

This method is used to insert an element into a certain position in the list, this method will change the original list, and the result will return the updated list

num_list = [1,2,3,4 ]
 #Insert 0 at index 0 
num_list.insert(0,0)
 print (num_list) #Output   : [0, 1, 2, 3, 4]

 

 

list.index(value,start=None,stop=None)

This method is used to retrieve whether the specified value is included in the list. By default, the entire list is retrieved from left to right, but the retrieval area can also be specified through the second and third parameters. If a value is retrieved, the value of the value will be returned immediately. The index of the table below, if not retrieved, an error will be reported.

num_list = [1,2,3,4]
num_index = num_list.index(3 )
 print (num_index) #Output   : 2

 

 

list.pop(index=None)

This method is used to delete elements in the list. By default, the last element of the list is deleted, but it can also be deleted by specifying the index. The result returns the deleted element. This method will change the original list.

num_list = [1,2,3,4 ]
 #Delete the last element by default 
element = num_list.pop()
 print (element)   #Output : 4 #Delete the second element in the list element = 
num_list.pop 
(1 )
 print ( element) #output   : 2

 

 

list.remove(value)

This method is used to delete a value in the list, this method will change the original list, the result returns None

num_list = [1,2,3,4]
num_list.remove( 2 )
 print (num_list) #Output   : [1, 3, 4]

 

 

list.reverse( )

This method is used to reverse the elements of the list. This method will change the original list and return the updated list

num_list = [1,2,3,4]
num_list.reverse()
print (num_list) #Output   : [4, 3, 2, 1]

 

 

list.sort(key=None,reverse=False)

This method is used to sort the elements in the list. By default, it is sorted in ascending order according to the Unicode code. Of course, it can also be sorted in descending order by reversing. This method will change the original list, and the result will return the updated list.

num_list = [3,2,4,1]
num_list.sort()
print (num_list) #Output   : [1, 2, 3, 4] 
num_list.sort(reverse= True)
 print (num_list) #Output   : [4, 3, 2, 1]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811718&siteId=291194637