Dictionary sorting problem
Duncan Booth
duncan.booth at invalid.invalid
Sat Sep 17 07:01:41 EDT 2005
Bengt Richter wrote:
> or tell sorted what to do ;-)
>
> >>> original= {
> ... 'hello':135,
> ... 'goodbye':30,
> ... 'lucy':4,
> ... 'sky':55,
> ... 'diamonds':239843,
> ... 'yesterday':4 }
> >>> list(sorted(original.iteritems(), None, lambda t:t[1], True))
> [('diamonds', 239843), ('hello', 135), ('sky', 55), ('goodbye', 30),
> ('yesterday', 4), ('lucy',4)]
or a slight variation on this theme which just gives you the keys in value
order rather than the tuples:
>>> for k in sorted(original, key=original.get, reverse=True):
print k, original[k]
diamonds 239843
hello 135
sky 55
goodbye 30
yesterday 4
lucy 4
More information about the Python-list
mailing list