Note: The test environment of this article is python2.7
Note: This article mainly introduces the general method of lists
Testing list
list1 = [1,2,3, 4]
insert method:
Method explanation: Insert the object at the specified position
Parameters:
Parameter 1: index
Parameter 2: object
Code example:
list1.insert(-1,1) #Insert object at the end of the list
list1.insert(0 ,1) #Insert object at the beginning of the list
Return value: None
Note: After the method is executed, the original list list1
pop method is directly modified :
Method explanation: Delete the element at the specified index
Parameters: If there is no parameter, the default is index -1 (the last member of the list)
Optional parameter one: index
Code example:
eg: s = list1.pop(1),s=list1[1] before deletion
Return value: deleted index Element
Note 1: When the index does not exist, an out-of-bounds error will be thrown
Note 2: The pop method can implement stack operations
eg:list1.append (list1.pop())
extend method:
Method explanation: Insert the sequence at the end of the list
Parameters:
Parameter 1: Sequence
Code example:
list2 = [1]
list1.extend(list2)
Return value: None
Note: Directly modify the original list list1, often used to connect lists, eg: list1 = list1+list2
index method:
Method explanation: return Specify the position where the 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 of the specified position of the specified object, you need to use the other two parameters
index(objetc,startindex,endindex)
startindex start index
endindex End index
Note: When the object does not exist, an error will be thrown
append method:
Method explanation: Insert data at the end of the list
Parameter:
Parameter 1: object
Code example:
list1.append(1)
Return value: None
count method:
Method explanation: Returns the number of times the specified object appears in the list
Parameters:
Parameter 1: object
Code example:
ret = list1.count(11)
Return value: ret (int>=0)
Note: It can also be based on the number of returns = 0, to determine that the object is not in the list
remove method:
Method explanation: delete the object that appears first in the list
Parameters:
Parameter 1: object
Return value:
Code example:
list1.count(1)
Note 1: The When there are multiple elements, only the first one will be deleted
Note: When the object does not exist, an error will be thrown. It is best to use the count method and in to make the judgment
reverse method:
Method explanation: Reverse the original list
Parameters:
No parameters
Code example:
list1 .reverse()
Return value: No return value
Note: Directly modify list1 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 the reverse sequence of list1, but it will not affect the original sequence list1
Note 2: The effect after conversion is similar to list1[::-1]
sort method:
Method explanation: ascending sequence or Sort in descending order
Code example:
list1.sort()
Note: Sort list1 in ascending order
list1.sort (reverse=True)
Note: Sort list1 in descending order
Note: Built-in function sorted()
Method explanation: Return the original sequence Sorted sequence
Code example:
list2 = sorted(list1)
Note: Sorting in ascending order will not affect the original sequence list1
list2 = sorted(list1,reverse=True)
Note: Sorting in descending order will not affect the original required list1
The above is the detailed content of General method of python sequence list. For more information, please follow other related articles on the PHP Chinese website!