Re: [Python-Dev] PEP 265 - Sorting dicts by value
13 Sep
2004
13 Sep
'04
11:46 a.m.
Moore, Paul wrote:
From: Nick Coghlan
In Python 2.4:
->>> ud = dict(a=1, b=2, c=3) ->>> from operator import itemgetter ->>> print sorted(ud.items(), key=itemgetter(1), reverse=True) [('c', 3), ('b', 2), ('a', 1)]
If you haven't done so already, I think this should be submitted to the cookbook. It's a nice idiom, and demonstrates some useful Python 2.4 features, and how they work well in combination.
It's submitted now. I have a feeling Raymond is the one who should get the credit for the approach, though. I'd be surprised if he made it through the discussions about the introduction of sorted without using this example at least once :) Cheers, Nick.
7420
Age (days ago)
7420
Last active (days ago)
0 comments
1 participants
participants (1)
-
Nick Coghlan