Data structures: from the LIFO list to the last in the

Usage scenarios: the need to update the data frequently used, new incoming data and comparing existing data, and then replaced by new incoming data of the original data, and so on. This process must be taken to switch the order of the old and new data, so the index in the list is critical.
List
Python list is variable, which is the most important feature distinguishes it from strings and tuples, namely one sentence: the list can be modified, while the strings and tuples can not.
The following is a list of the Python method:
Method and described
list.append (x) to add an element to the end of the list, corresponding to a [len (a):] = [x].
list.extend (L) to expand by adding all the elements of the specified list of lists, corresponding to a [len (a):] = L.
list.insert (i, x) to insert an element at the specified location. The first parameter is the index to be inserted into the front of which that element, e.g. a.insert (0, x) will be inserted before the entire list, and a.insert (len (a), x ) corresponds a.append ( x).
list.remove (x) removes the first element in the list whose value is x. If no such element, it returns an error.
list.pop ([i]) position of the specified element is removed from the list, and returns it. If you do not specify the index, a.pop () Returns the last element. Then elements are removed from the list. (I approach the square brackets denote that the parameter is optional, not that you should type square brackets, you will often encounter in the Python Library Reference such markers.)
List.clear () to remove the list All items, equal to del a [:].
list.index (x) returns the index of the first value x list element. If the element does not match the returns an error.
list.count (x) Returns the number of times x appears in the list.
list.sort () in the list of elements to be sorted.
list.reverse () elements in the list inverted.
list.copy () returns a list of the shallow copy, equal to a [:].

Using the list as a stack (LIFO)
list listing such method can be easily used as a stack, the stack as the specific data, where the first element is the last release (LIFO). It can be added with the append () method to a top of the stack element. Without an explicit index pop () method of an element can be released from the top of the stack. E.g:

>>> list_t=["a","b","c"]
>>> list_t.append("d")
>>> list_t.append("e")
>>> list_t
['a', 'b', 'c', 'd', 'e']
>>> list_t.pop()
'e'
>>> list_t
['a', 'b', 'c', 'd']
>>> list_t.pop()
'd'
>>> list_t
['a', 'b', 'c']

Used as a queue list (after the backward)
may be used to queue as a list, but in the first queue element is added, the first taken out; however such as to take the list of objects is not efficient. At the end of the list or add pop elements fast speed, but insert or eject from the head speed is not fast (because all the other elements have to move one by one) in the list

>>> from collections import deque
>>> list_t=deque(["a","b","c"])
>>> list_t.append("d")
>>> list_t
deque(['a', 'b', 'c', 'd'])
>>> list_t.popleft()
'a'
>>> list_t
deque(['b', 'c', 'd'])
>>>

Guess you like

Origin blog.51cto.com/chier11/2415967