Commit 981a9185 authored by Andrew M. Kuchling's avatar Andrew M. Kuchling

Various edits

parent b845ef05
...@@ -104,8 +104,8 @@ using a \var{key} parameter. Using \var{key} results in calling the ...@@ -104,8 +104,8 @@ 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.
Note, for simple key functions and comparison functions, it is often For simple key functions and comparison functions, it is often
possible to avoid the \keyword{lambda} expression by using an unbound possible to avoid a \keyword{lambda} expression by using an unbound
method instead. For example, the above case-insensitive sort is best method instead. For example, the above case-insensitive sort is best
coded as: coded as:
...@@ -120,10 +120,11 @@ The \var{reverse} parameter should have a Boolean value. If the value is ...@@ -120,10 +120,11 @@ The \var{reverse} parameter should have a Boolean value. If the value is
of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write: of \code{L.sort(lambda x,y: cmp(y.score, x.score))}, you can now write:
\code{L.sort(key = lambda x: x.score, reverse=True)}. \code{L.sort(key = lambda x: x.score, reverse=True)}.
The results of sorting are now guaranteed to be stable. This means that The results of sorting are now guaranteed to be stable. This means
two entries with equal keys will be returned in the same order as that two entries with equal keys will be returned in the same order as
they were input. they were input. For example, you can sort a list of people by name,
and then sort the list by age, resulting in a list sorted by age where
people with the same age are in name-sorted order.
\item The list type gained a \method{sorted(iterable)} method that works \item The list type gained a \method{sorted(iterable)} method that works
like the in-place \method{sort()} method but has been made suitable for like the in-place \method{sort()} method but has been made suitable for
...@@ -143,6 +144,7 @@ use in expressions. The differences are: ...@@ -143,6 +144,7 @@ use in expressions. The differences are:
>>> list.sorted('Monte Python') # any iterable may be an input >>> list.sorted('Monte Python') # any iterable may be an input
[' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y'] [' ', 'M', 'P', 'e', 'h', 'n', 'n', 'o', 'o', 't', 't', 'y']
>>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5) >>> colormap = dict(red=1, blue=2, green=3, black=4, yellow=5)
>>> # Lists the contents of the dict sorted by key values
>>> for k, v in list.sorted(colormap.iteritems()): >>> for k, v in list.sorted(colormap.iteritems()):
... print k, v ... print k, v
... ...
...@@ -293,6 +295,6 @@ changes to your code: ...@@ -293,6 +295,6 @@ changes to your code:
The author would like to thank the following people for offering The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this suggestions, corrections and assistance with various drafts of this
article: . article: Raymond Hettinger.
\end{document} \end{document}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment