sort dictionary by specific values

Gary Herron gherron at islandtraining.com
Sat Aug 18 14:54:59 EDT 2007


dorje tarap wrote:
> Hi,
>
> I need to perform some horrible functions in python I need to do,
> using sort in a similar way that Excel can.
>
> With a dictionary like:
> Code: ( text )
>
>   1.
>       >>> d
>   2.
>       {8: (99, 99), 9: [(55, 67), (77, 66), (67, 88)], 4: [(45, 78),
>       (56, 78), (99, 78)], 5: (67, 77)}
>
>
> I want to sort the entire dictionary based on the last values in each
> line. First for [-1][0] and then[-1][0].
>
> So sorted descending I would like the output to look like:
> Code: ( text )
>
>   1.
>       >>> d
>   2.
>       {8: (99, 99), 4: [(45, 78), (56, 78), (99, 78)], 9: [(55, 67),
>       (77, 66), (67, 88)], 5: (67, 77)}
>
>
> Many thanks 
I can't make any sense of your requirements or your example, but perhaps
I can help somewhat.

You CANNOT sort a (standard Python) dictionary.   You CAN find some
alternative implements of dictionaries on the web that allow ordering,
and you CAN extract the key,value pairs from a dictionary into a list,
and sort that list (by any criteria you want). 

Gary Herron




More information about the Python-list mailing list