Summary of common Python list operations, pythonlist
Common List object operation methods:
List. append (x)
Add an element to the end of the linked list, which is equivalent to a [len (a):] = [x].
List. extend (L)
Add all elements in a given list to another list, which is equivalent to a [len (a):] = L.
List. insert (I, x)
Insert an element at the specified position. The first parameter is the index of the element to be inserted before it, for example,. insert (0, x) is inserted before the entire linked list, and. insert (len (a), x) is equivalent to. append (x ).
List. remove (x)
Delete the first element whose median is x in the linked list. If no such element exists, an error is returned.
List. pop ([I])
Delete an element from the specified position of the linked list and return it. If no index is specified, a. pop () returns the last element. The element is deleted from the linked list. (Square brackets on both sides of method I indicate that this parameter is optional, rather than requiring you to enter a pair of square brackets. You will often encounter this mark in the Python Library Reference Manual .)
List. index (x)
Returns the index of the first x element in the linked list. If no matching element exists, an error is returned.
List. count (x)
Returns the number of times x appears in the linked list.
List. sort ()
Sort the elements in the linked list in the local area.
List. reverse ()
In-Place inverted list elements.
Example:
Copy codeThe Code is as follows:
>>> A = [66.25, 333,333, 1, 1234.5]
>>> Print a. count (333), a. count (66.25), a. count ('x ')
2 1 0
>>> A. insert (2,-1)
>>> A. append (333)
>>>
[66.25, 333,-1,333, 1, 1234.5, 333]
>>> A. index (333)
1
>>> A. remove (333)
>>> A. index (333)
2
>>>
[66.25,-1,333, 1, 1234.5, 333]
>>> A. reverse ()
>>>
[333,123 4.5, 1,333,-1, 66.25]
>>> A. sort ()
>>>
[-1, 1, 66.25, 333,333,123 4.5]