| 1 | \section{\module{collections} ---
|
|---|
| 2 | High-performance container datatypes}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{collections}
|
|---|
| 5 | \modulesynopsis{High-performance datatypes}
|
|---|
| 6 | \moduleauthor{Raymond Hettinger}{[email protected]}
|
|---|
| 7 | \sectionauthor{Raymond Hettinger}{[email protected]}
|
|---|
| 8 | \versionadded{2.4}
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | This module implements high-performance container datatypes. Currently,
|
|---|
| 12 | there are two datatypes, deque and defaultdict.
|
|---|
| 13 | Future additions may include balanced trees and ordered dictionaries.
|
|---|
| 14 | \versionchanged[Added defaultdict]{2.5}
|
|---|
| 15 |
|
|---|
| 16 | \subsection{\class{deque} objects \label{deque-objects}}
|
|---|
| 17 |
|
|---|
| 18 | \begin{funcdesc}{deque}{\optional{iterable}}
|
|---|
| 19 | Returns a new deque objected initialized left-to-right (using
|
|---|
| 20 | \method{append()}) with data from \var{iterable}. If \var{iterable}
|
|---|
| 21 | is not specified, the new deque is empty.
|
|---|
| 22 |
|
|---|
| 23 | Deques are a generalization of stacks and queues (the name is pronounced
|
|---|
| 24 | ``deck'' and is short for ``double-ended queue''). Deques support
|
|---|
| 25 | thread-safe, memory efficient appends and pops from either side of the deque
|
|---|
| 26 | with approximately the same \code{O(1)} performance in either direction.
|
|---|
| 27 |
|
|---|
| 28 | Though \class{list} objects support similar operations, they are optimized
|
|---|
| 29 | for fast fixed-length operations and incur \code{O(n)} memory movement costs
|
|---|
| 30 | for \samp{pop(0)} and \samp{insert(0, v)} operations which change both the
|
|---|
| 31 | size and position of the underlying data representation.
|
|---|
| 32 | \versionadded{2.4}
|
|---|
| 33 | \end{funcdesc}
|
|---|
| 34 |
|
|---|
| 35 | Deque objects support the following methods:
|
|---|
| 36 |
|
|---|
| 37 | \begin{methoddesc}{append}{x}
|
|---|
| 38 | Add \var{x} to the right side of the deque.
|
|---|
| 39 | \end{methoddesc}
|
|---|
| 40 |
|
|---|
| 41 | \begin{methoddesc}{appendleft}{x}
|
|---|
| 42 | Add \var{x} to the left side of the deque.
|
|---|
| 43 | \end{methoddesc}
|
|---|
| 44 |
|
|---|
| 45 | \begin{methoddesc}{clear}{}
|
|---|
| 46 | Remove all elements from the deque leaving it with length 0.
|
|---|
| 47 | \end{methoddesc}
|
|---|
| 48 |
|
|---|
| 49 | \begin{methoddesc}{extend}{iterable}
|
|---|
| 50 | Extend the right side of the deque by appending elements from
|
|---|
| 51 | the iterable argument.
|
|---|
| 52 | \end{methoddesc}
|
|---|
| 53 |
|
|---|
| 54 | \begin{methoddesc}{extendleft}{iterable}
|
|---|
| 55 | Extend the left side of the deque by appending elements from
|
|---|
| 56 | \var{iterable}. Note, the series of left appends results in
|
|---|
| 57 | reversing the order of elements in the iterable argument.
|
|---|
| 58 | \end{methoddesc}
|
|---|
| 59 |
|
|---|
| 60 | \begin{methoddesc}{pop}{}
|
|---|
| 61 | Remove and return an element from the right side of the deque.
|
|---|
| 62 | If no elements are present, raises an \exception{IndexError}.
|
|---|
| 63 | \end{methoddesc}
|
|---|
| 64 |
|
|---|
| 65 | \begin{methoddesc}{popleft}{}
|
|---|
| 66 | Remove and return an element from the left side of the deque.
|
|---|
| 67 | If no elements are present, raises an \exception{IndexError}.
|
|---|
| 68 | \end{methoddesc}
|
|---|
| 69 |
|
|---|
| 70 | \begin{methoddesc}{remove}{value}
|
|---|
| 71 | Removed the first occurrence of \var{value}. If not found,
|
|---|
| 72 | raises a \exception{ValueError}.
|
|---|
| 73 | \versionadded{2.5}
|
|---|
| 74 | \end{methoddesc}
|
|---|
| 75 |
|
|---|
| 76 | \begin{methoddesc}{rotate}{n}
|
|---|
| 77 | Rotate the deque \var{n} steps to the right. If \var{n} is
|
|---|
| 78 | negative, rotate to the left. Rotating one step to the right
|
|---|
| 79 | is equivalent to: \samp{d.appendleft(d.pop())}.
|
|---|
| 80 | \end{methoddesc}
|
|---|
| 81 |
|
|---|
| 82 | In addition to the above, deques support iteration, pickling, \samp{len(d)},
|
|---|
| 83 | \samp{reversed(d)}, \samp{copy.copy(d)}, \samp{copy.deepcopy(d)},
|
|---|
| 84 | membership testing with the \keyword{in} operator, and subscript references
|
|---|
| 85 | such as \samp{d[-1]}.
|
|---|
| 86 |
|
|---|
| 87 | Example:
|
|---|
| 88 |
|
|---|
| 89 | \begin{verbatim}
|
|---|
| 90 | >>> from collections import deque
|
|---|
| 91 | >>> d = deque('ghi') # make a new deque with three items
|
|---|
| 92 | >>> for elem in d: # iterate over the deque's elements
|
|---|
| 93 | ... print elem.upper()
|
|---|
| 94 | G
|
|---|
| 95 | H
|
|---|
| 96 | I
|
|---|
| 97 |
|
|---|
| 98 | >>> d.append('j') # add a new entry to the right side
|
|---|
| 99 | >>> d.appendleft('f') # add a new entry to the left side
|
|---|
| 100 | >>> d # show the representation of the deque
|
|---|
| 101 | deque(['f', 'g', 'h', 'i', 'j'])
|
|---|
| 102 |
|
|---|
| 103 | >>> d.pop() # return and remove the rightmost item
|
|---|
| 104 | 'j'
|
|---|
| 105 | >>> d.popleft() # return and remove the leftmost item
|
|---|
| 106 | 'f'
|
|---|
| 107 | >>> list(d) # list the contents of the deque
|
|---|
| 108 | ['g', 'h', 'i']
|
|---|
| 109 | >>> d[0] # peek at leftmost item
|
|---|
| 110 | 'g'
|
|---|
| 111 | >>> d[-1] # peek at rightmost item
|
|---|
| 112 | 'i'
|
|---|
|
|---|