|
27 | 27 |
|
28 | 28 |
|
29 | 29 | %====================================================================== |
| 30 | +\section{PEP 322: Reverse Iteration} |
30 | 31 |
|
31 | | -% Large, PEP-level features and changes should be described here. |
| 32 | +A new built-in function, \function{reversed(seq)}, takes a sequence |
| 33 | +and returns an iterator that returns the elements of the sequence |
| 34 | +in reverse order. |
| 35 | + |
| 36 | +\begin{verbatim} |
| 37 | +>>> for i in reversed([1,2,3]): |
| 38 | +... print i |
| 39 | +... |
| 40 | +3 |
| 41 | +2 |
| 42 | +1 |
| 43 | +\end{verbatim} |
| 44 | + |
| 45 | +Note that \function{reversed()} only accepts sequences, not arbitrary |
| 46 | +iterators. If you want to reverse an iterator, convert it to |
| 47 | +a list or tuple with \function{list()} or \function{tuple()}. |
| 48 | + |
| 49 | +\begin{verbatim} |
| 50 | +>>> input = open('/etc/passwd', 'r') |
| 51 | +>>> for line in reversed(list(input)): |
| 52 | +... print line |
| 53 | +... |
| 54 | +root:*:0:0:System Administrator:/var/root:/bin/tcsh |
| 55 | + ... |
| 56 | +\end{verbatim} |
32 | 57 |
|
33 | 58 |
|
34 | 59 | %====================================================================== |
@@ -76,6 +101,23 @@ \section{Other Language Changes} |
76 | 101 | of \code{L.sort() ; L.reverse()}, you can now write |
77 | 102 | \code{L.sort(reverse=True)}. |
78 | 103 |
|
| 104 | +\item The list type gained a \method{sorted(iterable)} method that |
| 105 | +returns the elements of the iterable as a sorted list. It also accepts |
| 106 | +the \var{cmp}, \var{key}, and \var{reverse} keyword arguments, same as |
| 107 | +the \method{sort()} method. An example usage: |
| 108 | + |
| 109 | +\begin{verbatim} |
| 110 | +>>> L = [9,7,8,3,2,4,1,6,5] |
| 111 | +>>> list.sorted(L) |
| 112 | +[1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 113 | +>>> L |
| 114 | +[9, 7, 8, 3, 2, 4, 1, 6, 5] |
| 115 | +>>> |
| 116 | +\end{verbatim} |
| 117 | + |
| 118 | +Note that the original list is unchanged; the list returned by |
| 119 | +\method{sorted()} is a newly-created one. |
| 120 | + |
79 | 121 | \item The \function{zip()} built-in function and \function{itertools.izip()} now return an empty list |
80 | 122 | instead of raising a \exception{TypeError} exception if called |
81 | 123 | with no arguments. |
@@ -114,6 +156,9 @@ \section{New, Improved, and Deprecated Modules} |
114 | 156 | supports transparency, this makes it possible to use a transparent background. |
115 | 157 | (Contributed by J\"org Lehmann.) |
116 | 158 |
|
| 159 | +\item The \module{heapq} module is no longer implemented in Python, |
| 160 | + having been converted into C. |
| 161 | + |
117 | 162 | \item The \module{random} module has a new method called \method{getrandbits(N)} |
118 | 163 | which returns an N-bit long integer. |
119 | 164 |
|
|
0 commit comments