[Python-checkins] python/dist/src/Doc/whatsnew whatsnew24.tex, 1.3, 1.4

akuchling at users.sourceforge.net akuchling at users.sourceforge.net
Tue Oct 21 08:31:18 EDT 2003


Update of /cvsroot/python/python/dist/src/Doc/whatsnew
In directory sc8-pr-cvs1:/tmp/cvs-serv10773

Modified Files:
	whatsnew24.tex 
Log Message:
Document list.sort() changes

Index: whatsnew24.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/whatsnew/whatsnew24.tex,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** whatsnew24.tex	20 Sep 2003 15:52:20 -0000	1.3
--- whatsnew24.tex	21 Oct 2003 12:31:16 -0000	1.4
***************
*** 39,43 ****
  
  \begin{itemize}
! \item TBD
  
  \end{itemize}
--- 39,79 ----
  
  \begin{itemize}
! \item The \method{sort()} method of lists gained three keyword
! arguments, \var{cmp}, \var{key}, and \var{reverse}.  These arguments
! make some common usages of \method{sort()} simpler.  All are optional.
! 
! \var{cmp} is the same as the previous single argument to
! \method{sort()}; if provided, the value should be a comparison
! function that takes two arguments and returns -1, 0, or +1 depending
! on how the arguments compare.  
! 
! \var{key} should be a single-argument function that takes a list
! element and returns a comparison key for the element.  The list is
! then sorted using the comparison keys.  The following example sorts a list
! case-insensitively:
! 
! \begin{verbatim}
! >>> L = ['A', 'b', 'c', 'D']
! >>> L.sort()                 # Case-sensitive sort
! >>> L
! ['A', 'D', 'b', 'c']
! >>> L.sort(key=lambda x: x.lower())
! >>> L
! ['A', 'b', 'c', 'D']
! >>> L.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()))
! >>> L
! ['A', 'b', 'c', 'D']
! \end{verbatim}
! 
! 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
! using a \var{key} parameter.  Using \var{key} results in calling the
! \method{lower()} method once for each element in the list while using
! \var{cmp} will call the method twice for each comparison.
! 
! The \var{reverse} parameter should have a Boolean value.  If the value is
! \constant{True}, the list will be sorted into reverse order.  Instead
! of \code{L.sort() ; L.reverse()}, you can now write
! \code{L.sort(reverse=True)}.
  
  \end{itemize}





More information about the Python-checkins mailing list