Note: This test environment is python2.7
Note: This article mainly introduces the general method of the list
Test List
List1 = [1,2,3,4]
Insert Method:
Method Explanation: Inserting an object at a specified location
Parameters:
Parameter 1:index
Parameter 2:object
code example:
List1.insert ( -1,1) #在列表末尾插入对象
List1.insert (0,1) #在列表开头插入对象
return value: None
Note: After the method executes, directly modifies the original list List1
Pop method:
Method Explanation: Delete the element of the specified index
Parameter: Default is index-1 (last member of list) without parameter
Optional parameter one: index
code example:
Eg:s = List1.pop (1), s= before deletion list1[1]
Return value: The element at the index being deleted
Note 1: A cross-border error is thrown when the index does not exist
Note the 2:pop method can implement the operation of the stack
Eg:list1.append (List1.pop ())
Extend method:
Method explanation: Inserting a sequence at the end of a list
Parameters:
Parameter one: sequence
code example:
List2 = [1]
List1.extend (LIST2)
return value: None
Note: Modify the original list List1 directly, commonly used for connection list, Eg:list1 = List1+list2
Index method:
Method Explanation: Returns the position where the specified element first appears in the list
Parameters:
Parameter 1:object
Parameter 2:startindex (optional, default 0)
Parameter 3:endindex (optional)
code example:
List1[0] = 1
Then List1.index (1) = 0
If you want to output the index at the specified position of the specified object, you need to use two additional parameters
Index (OBJETC,STARTINDEX,ENDINDEX)
startindex Start Index
Endindex End Index
Note: An error is thrown when the object does not exist
Append method:
Method Explanation: Inserting data at the end of the list
Parameters:
Parameter 1:object
code example:
List1.append (1)
return value: None
Count Method:
Method Explanation: Returns the number of occurrences of the specified object in the list
Parameters:
Parameter 1:object
code example:
ret = List1.count (11)
return value: Ret (int>=0)
Note: It is also possible to determine if the object is not in the list, depending on the number of returns = 0
Remove method:
Method Explanation: Delete the first occurrence of the object in the list
Parameters:
Parameter 1:object
return value:
code example:
List1.count (1)
Note 1: When the element has multiple, only the first one is deleted
Note: When an object does not exist, it throws an error, preferably with the count method and in to determine
Reverse method:
Method Explanation: Invert the original list
Parameters:
No reference
code example:
List1.reverse ()
Return value: no return value
Note: Modify the List1 directly to reverse the sequence
Note: Built-in function reversed (object)
Parameter 1:
Sequence
code example:
List1 = [1,2,3,4]
List2 = List (Reversed (List1))
return value: Iterator
Note: The returned list2 is an inverted sequence of list1, but does not affect the original sequence List1
Note 2: The effect after conversion is similar to list1[::-1]
Sort method:
Method Explanation: Sort the sequence in ascending or descending order
code example:
List1.sort ()
Note: The list1 is sorted in ascending order
List1.sort (Reverse=true)
Note: The list1 is sorted in descending order
Note: Built-in function sorted ()
Method Explanation: Returns the sequence after the original sequence is sorted
code example:
List2 = sorted (List1)
Note: Ascending arrangement does not affect the original sequence List1
List2 = sorted (list1,reverse=true)
Note: The descending arrangement does not affect the original need List1