Help with dictionary

Peter Abel p-abel at t-online.de
Thu Jun 5 06:07:22 EDT 2003


iamshady at rediffmail.com (Jim Shady) wrote in message news:<9afd3f36.0306042301.5243a9e8 at posting.google.com>...
> Hello,
> 
> I have a dictionary of the sort:
> 
> {(1, (21353, 21418)): 3900, (2, (53006, 53164)): 3800, (0, (19697,
> 19763)): 4100}
> 
> I need to re-sort this dictionary with the first element of the tuple
> in the keys. A sorted dictionary of the above would be:
> 
A dictonary can't be sorted. If it once is, it happens by chance.
> {(0, (19697, 19763)): 4100, (1, (21353, 21418)): 3900, (2, (53006,
> 53164)): 3800}
> 
> Could somebody help me with the algorithm/logic to accomplish this?
What you have to do is to put the items of a dictionary into a list
which you can sort.
>>> myDict={(1, (21353, 21418)): 3900, (2, (53006, 53164)): 3800, (0,
(19697,19763)): 4100}
>>> myDict
{(2, (53006, 53164)): 3800, (0, (19697, 19763)): 4100, (1, (21353,
21418)): 3900}
>>> myItems=myDict.items()
>>> myItems.sort()
>>> myItems
[((0, (19697, 19763)), 4100), ((1, (21353, 21418)), 3900), ((2,
(53006, 53164)), 3800)]
>>> 
Regards
Peter
> 
> Thanks in advance!
> 
> Jim




More information about the Python-list mailing list