Re: [Tutor] samples on sort method of sequence object.

2010-01-12 Thread Kent Johnson
On Tue, Jan 12, 2010 at 9:39 AM, Make Twilight  wrote:

>  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?

http://personalpages.tds.net/~kent37/kk/7.html
Kent
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] samples on sort method of sequence object.

2010-01-12 Thread Hugo Arts
On Tue, Jan 12, 2010 at 3:39 PM, Make Twilight  wrote:
> Hi,there:
>  The document of
> Python.org[http://docs.python.org/library/stdtypes.html] says that:
>
> 
>
>  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!
>

key has essentially the same purpose as cmp, but it works more
efficiently because it only calls the key function once for each value
in the sequence. A simple though maybe not very useful example could
be sorting lists of integers in such a way that the list with the
lowest sum comes first:

>>> seqs = [(2, 0, 0), (3, 5, 5), (1, 100, 100)]
>>> seqs.sort()
>>> seqs
[(1, 100, 100), (2, 0, 0), (3, 5, 5)]
>>> seqs.sort(key=sum)
>>> seqs
[(2, 0, 0), (3, 5, 5), (1, 100, 100)]

the sort function calls the key function sum(s) for every s in
sequence, then sorts the list using the result of that call. An
equivalent example using the cmp function could be this one:

>>> seqs.sort(cmp=lambda x, y: cmp(sum(x), sum(y))

Another example given by the docs is using the str.lower function.
Here it is in action:

>>> s = list("A string WitH Some Capitals")
>>> s.sort(key=str.lower)
>>> s
[' ', ' ', ' ', ' ', 'A', 'a', 'a', 'C', 'e', 'g', 'H', 'i', 'i', 'i',
'l', 'm', 'n', 'o', 'p', 'r', 's', 'S', 's', 't', 't', 't', 'W']
>>> s.sort()
>>> s
[' ', ' ', ' ', ' ', 'A', 'C', 'H', 'S', 'W', 'a', 'a', 'e', 'g', 'i',
'i', 'i', 'l', 'm', 'n', 'o', 'p', 'r', 's', 's', 't', 't', 't']

Normally, when sorting a string, capital letters come before regular
ones. With the key() argument, the sorting is done between the lowered
versions of each letter, so the sort becomes case insensitive. Note
that this doesn't actually change our string, it only changes what
exactly is used for our comparisons. Again, same sort using the cmp
argument:

s.sort(cmp=lambda x, y: cmp(x.lower(), y.lower())

Hugo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] samples on sort method of sequence object.

2010-01-12 Thread Make Twilight
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!


--
ph4...@gmail.com
Mak Twilight
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor