mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Various fixups:
* Add comment on the future of the sets module. * Change a variable from "input" to "data" to avoid shadowing a builtin. * Added possible applications for str.rsplit() and itertools.tee(). * Repaired the example for sorted(). * Cleaned-up the example for operator.itemgetter().
This commit is contained in:
parent
32fef9f477
commit
ed54d91ef5
1 changed files with 36 additions and 26 deletions
|
@ -70,6 +70,10 @@ as a member of another set. Accordingly, it does not have methods
|
||||||
like \method{add()} and \method{remove()} which could alter its contents.
|
like \method{add()} and \method{remove()} which could alter its contents.
|
||||||
|
|
||||||
% XXX what happens to the sets module?
|
% XXX what happens to the sets module?
|
||||||
|
% The current thinking is that the sets module will be left alone.
|
||||||
|
% That way, existing code will continue to run without alteration.
|
||||||
|
% Also, the module provides an autoconversion feature not supported by set()
|
||||||
|
% and frozenset().
|
||||||
|
|
||||||
\begin{seealso}
|
\begin{seealso}
|
||||||
\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
|
\seepep{218}{Adding a Built-In Set Object Type}{Originally proposed by
|
||||||
|
@ -105,8 +109,8 @@ iterators. If you want to reverse an iterator, first convert it to
|
||||||
a list with \function{list()}.
|
a list with \function{list()}.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> input = open('/etc/passwd', 'r')
|
>>> data = open('/etc/passwd', 'r')
|
||||||
>>> for line in reversed(list(input)):
|
>>> for line in reversed(list(data)):
|
||||||
... print line
|
... print line
|
||||||
...
|
...
|
||||||
root:*:0:0:System Administrator:/var/root:/bin/tcsh
|
root:*:0:0:System Administrator:/var/root:/bin/tcsh
|
||||||
|
@ -132,7 +136,9 @@ language.
|
||||||
fill character other than a space.
|
fill character other than a space.
|
||||||
|
|
||||||
\item Strings also gained an \method{rsplit()} method that
|
\item Strings also gained an \method{rsplit()} method that
|
||||||
works like the \method{split()} method but splits from the end of the string.
|
works like the \method{split()} method but splits from the end of
|
||||||
|
the string. Possible applications include splitting a filename
|
||||||
|
from a path or a domain name from URL.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> 'a b c'.split(None, 1)
|
>>> 'a b c'.split(None, 1)
|
||||||
|
@ -169,7 +175,7 @@ list case-insensitively:
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
The last example, which uses the \var{cmp} parameter, is the old way
|
The last example, which uses the \var{cmp} parameter, is the old way
|
||||||
to perform a case-insensitive sort. It works, but is slower than
|
to perform a case-insensitive sort. It works but is slower than
|
||||||
using a \var{key} parameter. Using \var{key} results in calling the
|
using a \var{key} parameter. Using \var{key} results in calling the
|
||||||
\method{lower()} method once for each element in the list while using
|
\method{lower()} method once for each element in the list while using
|
||||||
\var{cmp} will call the method twice for each comparison.
|
\var{cmp} will call the method twice for each comparison.
|
||||||
|
@ -230,7 +236,7 @@ yellow 5
|
||||||
|
|
||||||
\item The \function{zip()} built-in function and \function{itertools.izip()}
|
\item The \function{zip()} built-in function and \function{itertools.izip()}
|
||||||
now return an empty list instead of raising a \exception{TypeError}
|
now return an empty list instead of raising a \exception{TypeError}
|
||||||
exception if called with no arguments. This makes the functions more
|
exception if called with no arguments. This makes the function more
|
||||||
suitable for use with variable length argument lists:
|
suitable for use with variable length argument lists:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
|
@ -319,36 +325,41 @@ counting, or identifying duplicate elements:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> word = 'abracadabra'
|
>>> word = 'abracadabra'
|
||||||
>>> letters = sorted(word) # Turn string into sorted list of letters
|
>>> letters = sorted(word) # Turn string into a sorted list of letters
|
||||||
>>> letters
|
>>> letters
|
||||||
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
|
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r']
|
||||||
>>> [k for k, g in groupby(word)] # List unique letters
|
>>> [k for k, g in groupby(letters)] # List unique letters
|
||||||
['a', 'b', 'c', 'd', 'r']
|
['a', 'b', 'c', 'd', 'r']
|
||||||
>>> [(k, len(list(g))) for k, g in groupby(word)] # Count letter occurences
|
>>> [(k, len(list(g))) for k, g in groupby(letters)] # Count letter occurences
|
||||||
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
|
[('a', 5), ('b', 2), ('c', 1), ('d', 1), ('r', 2)]
|
||||||
>>> [k for k, g in groupby(word) if len(list(g)) > 1] # List duplicate letters
|
>>> [k for k, g in groupby(letters) if len(list(g)) > 1] # List duplicated letters
|
||||||
['a', 'b', 'r']
|
['a', 'b', 'r']
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
\item \module{itertools} also gained a function named \function{tee(\var{iterator}, \var{N})} that returns \var{N} independent iterators
|
\item \module{itertools} also gained a function named
|
||||||
that replicate \var{iterator}. If \var{N} is omitted, the default is
|
\function{tee(\var{iterator}, \var{N})} that returns \var{N} independent
|
||||||
2.
|
iterators that replicate \var{iterator}. If \var{N} is omitted, the
|
||||||
|
default is 2.
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> L = [1,2,3]
|
>>> L = [1,2,3]
|
||||||
>>> i1, i2 = itertools.tee(L)
|
>>> i1, i2 = itertools.tee(L)
|
||||||
>>> i1,i2
|
>>> i1,i2
|
||||||
(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
|
(<itertools.tee object at 0x402c2080>, <itertools.tee object at 0x402c2090>)
|
||||||
>>> list(i1)
|
>>> list(i1) # Run the first iterator to exhaustion
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
>>> list(i2)
|
>>> list(i2) # Run the second iterator to exhaustion
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
>\end{verbatim}
|
>\end{verbatim}
|
||||||
|
|
||||||
Note that \function{tee()} has to keep copies of the values returned
|
Note that \function{tee()} has to keep copies of the values returned
|
||||||
by the iterator; in the worst case it may need to keep all of them.
|
by the iterator; in the worst case, it may need to keep all of them.
|
||||||
This should therefore be used carefully if \var{iterator}
|
This should therefore be used carefully if there the leading iterator
|
||||||
returns a very large stream of results.
|
can run far ahead of the trailing iterator in a long stream of inputs.
|
||||||
|
If the separation is large, then it becomes preferrable to use
|
||||||
|
\function{list()} instead. When the iterators track closely with one
|
||||||
|
another, \function{tee()} is ideal. Possible applications include
|
||||||
|
bookmarking, windowing, or lookahead iterators.
|
||||||
|
|
||||||
\item A new \function{getsid()} function was added to the
|
\item A new \function{getsid()} function was added to the
|
||||||
\module{posix} module that underlies the \module{os} module.
|
\module{posix} module that underlies the \module{os} module.
|
||||||
|
@ -357,26 +368,25 @@ returns a very large stream of results.
|
||||||
\item The \module{operator} module gained two new functions,
|
\item The \module{operator} module gained two new functions,
|
||||||
\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}.
|
\function{attrgetter(\var{attr})} and \function{itemgetter(\var{index})}.
|
||||||
Both functions return callables that take a single argument and return
|
Both functions return callables that take a single argument and return
|
||||||
the corresponding attribute or item; these callables are handy for use
|
the corresponding attribute or item; these callables make excellent
|
||||||
with \function{map()} or \function{list.sort()}. For example, here's a simple
|
data extractors when used with \function{map()} or \function{sorted()}.
|
||||||
us
|
For example:
|
||||||
|
|
||||||
\begin{verbatim}
|
\begin{verbatim}
|
||||||
>>> L = [('c', 2), ('d', 1), ('a', '4'), ('b', 3)]
|
>>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
|
||||||
>>> map(operator.itemgetter(0), L)
|
>>> map(operator.itemgetter(0), L)
|
||||||
['c', 'd', 'a', 'b']
|
['c', 'd', 'a', 'b']
|
||||||
>>> map(operator.itemgetter(1), L)
|
>>> map(operator.itemgetter(1), L)
|
||||||
[2, 1, '4', 3]
|
[2, 1, 4, 3]
|
||||||
>>> L.sort(key=operator.itemgetter(1)) # Sort list by second item in tuples
|
>>> sorted(L, key=operator.itemgetter(1)) # Sort list by second tuple item
|
||||||
>>> L
|
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]
|
||||||
[('d', 1), ('c', 2), ('b', 3), ('a', '4')]
|
|
||||||
\end{verbatim}
|
\end{verbatim}
|
||||||
|
|
||||||
\item The \module{random} module has a new method called \method{getrandbits(N)}
|
\item The \module{random} module has a new method called \method{getrandbits(N)}
|
||||||
which returns an N-bit long integer. This method supports the existing
|
which returns an N-bit long integer. This method supports the existing
|
||||||
\method{randrange()} method, making it possible to efficiently generate
|
\method{randrange()} method, making it possible to efficiently generate
|
||||||
arbitrarily large random numbers (suitable for prime number generation in
|
arbitrarily large random numbers (suitable for prime number generation in
|
||||||
RSA applications).
|
RSA applications for example).
|
||||||
|
|
||||||
\item The regular expression language accepted by the \module{re} module
|
\item The regular expression language accepted by the \module{re} module
|
||||||
was extended with simple conditional expressions, written as
|
was extended with simple conditional expressions, written as
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue