[Tutor] Kent's top poster script operator.itemgetter()

Kent Johnson kent37 at tds.net
Sat Jan 3 17:46:09 CET 2009


On Sat, Jan 3, 2009 at 9:34 AM, Sander Sweers <sander.sweers at gmail.com> wrote:
> On Sat, Jan 3, 2009 at 05:15, Kent Johnson <kent37 at tds.net> wrote:
>> On Fri, Jan 2, 2009 at 6:12 PM, Sander Sweers <sander.sweers at gmail.com> wrote:
>>> not understand how  operator.itemgetter(1) works.
>>
>> See
>> http://personalpages.tds.net/~kent37/kk/00007.html#e7the-operator-module
>
> Ok, if I understand this correctly counts.iteritems() creates a tuple
> (name, count) and this is passed to operator.itemgetter() which take
> the second value to sort the list.

Almost right. The (name, count) tuple is passed to the function
returned by operator.itemgetter().
>
> Previously I used a function with cmp to sort datetime.date objects
> but this works way better :-)

Yes, the key= parameter to sort() pretty much obsoletes the use of a
comparison function.

Kent


More information about the Tutor mailing list