[Python-checkins] python/dist/src/Doc/lib libstdtypes.tex,1.120,1.121

fdrake@users.sourceforge.net fdrake@users.sourceforge.net
Thu, 20 Mar 2003 14:18:02 -0800


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

Modified Files:
	libstdtypes.tex 
Log Message:
- added example of using a comparison function with list.sort(), and
  explained the construction of a [(key, value), ...] list as an
  alternative
- note that support for cmpfunc=None was added in 2.3


Index: libstdtypes.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstdtypes.tex,v
retrieving revision 1.120
retrieving revision 1.121
diff -C2 -d -r1.120 -r1.121
*** libstdtypes.tex	6 Mar 2003 23:54:27 -0000	1.120
--- libstdtypes.tex	20 Mar 2003 22:17:59 -0000	1.121
***************
*** 1000,1009 ****
    the first argument is considered smaller than, equal to, or larger
    than the second argument.  Note that this slows the sorting process
!   down considerably; e.g. to sort a list in reverse order it is much
!   faster to call method \method{sort()} followed by \method{reverse()}
!   than to use method \method{sort()} with a comparison function that
    reverses the ordering of the elements.  Passing \constant{None} as the
    comparison function is semantically equivalent to calling
    \method{sort()} with no comparison function.
  
  \item[(9)] Whether the \method{sort()} method is stable is not defined by
--- 1000,1031 ----
    the first argument is considered smaller than, equal to, or larger
    than the second argument.  Note that this slows the sorting process
!   down considerably; for example to sort a list in reverse order it is much
!   faster to call \method{sort()} followed by \method{reverse()}
!   than to use \method{sort()} with a comparison function that
    reverses the ordering of the elements.  Passing \constant{None} as the
    comparison function is semantically equivalent to calling
    \method{sort()} with no comparison function.
+   \versionchanged[Support for \code{None} as an equivalent to omitting
+   \var{cmpfunc} was added]{2.3}
+ 
+   As an example of using the \var{cmpfunc} argument to the
+   \method{sort()} method, consider sorting a list of sequences by the
+   second element of that list:
+ 
+ \begin{verbatim}
+ def mycmp(a, b):
+     return cmp(a[1], b[1])
+ 
+ mylist.sort(mycmp)
+ \end{verbatim}
+ 
+   A more time-efficient approach for reasonably-sized data structures can
+   often be used:
+ 
+ \begin{verbatim}
+ tmplist = [(x[1], x) for x in mylist]
+ tmplist.sort()
+ mylist = [x for (key, x) in tmplist]
+ \end{verbatim}
  
  \item[(9)] Whether the \method{sort()} method is stable is not defined by