[Tutor] samples on sort method of sequence object.

Make Twilight ph4nut at gmail.com
Tue Jan 12 15:39:11 CET 2010


Hi,there:
  The document of
Python.org[http://docs.python.org/library/stdtypes.html] says that:
  ------------------------------------------------------------------------------------------------------------------
  s.sort([cmp[, key[, reverse]]])

  8. The sort() method takes optional arguments for controlling the comparisons.

      cmp specifies a custom comparison function of two arguments
(list items) which should return a negative, zero or positive number
depending on whether the first argument is considered smaller than,
equal to, or larger than the second argument: cmp=lambda x,y:
cmp(x.lower(), y.lower()). The default value is None.

     key specifies a function of one argument that is used to extract
a comparison key from each list element: key=str.lower. The default
value is None.

     reverse is a boolean value. If set to True, then the list
elements are sorted as if each comparison were reversed.
  --------------------------------------------------------------------------------------------------------------------
  I can understand how to use parameters of cmp and reverse,except the
key parameter...
  Would anyone give me an example of using sort method with key parameter?
  Thanks!


--
ph4nut at gmail.com
Mak Twilight


More information about the Tutor mailing list