[Tutor] Sort pointers -

Kent Johnson kent37 at tds.net
Wed Nov 24 12:05:47 CET 2004


As you have discovered, you can't directly sort a dict. You can only 
sort a mutable sequence such as a list. You can easily get a list from a 
dict in three ways:
d.keys() is a list of the keys
d.values() is a list of the values
d.items() is a list of (key, value) tuples

For example:
 >>> mp= {'James Bond' : '007' , 'Enid Blyton' : '005' , 'Enid Blyton 
also' : '006' , 'Captain Planet' : '000' }

 >>> mp.keys()
['Enid Blyton also', 'Captain Planet', 'James Bond', 'Enid Blyton']
 >>> mp.values()
['006', '000', '007', '005']
 >>> mp.items()
[('Enid Blyton also', '006'), ('Captain Planet', '000'), ('James Bond', 
'007'), ('Enid Blyton', '005')]

If you want to print the dict in order by key, it's easy - get the items 
list and sort that. Sorting a list of tuples will sort by the first item 
in the tuple.

Important note: Lists are sorted in place, and sort() doesn't return any 
value. So you have to assign mp.items() to a name, sort it, and print it 
as three steps:

 >>> it = mp.items()
 >>> it.sort()
 >>> it
[('Captain Planet', '000'), ('Enid Blyton', '005'), ('Enid Blyton also', 
'006'), ('James Bond', '007')]

If you want to sort by something other than the first element, it gets 
more complicated and there are several options.

I recommend using Python 2.4 which has an enhanced sort that greatly 
simplifies this common need. In P 2.4, sort() has an optional key 
argument that is a function to use to extract a key. The P 2.4 operator 
module defines an itemgetter function that makes functions to extract an 
indexed item from a list. So in P 2.4 you can sort your dict by the 
value like this:

 >>> import operator
 >>> it.sort(key=operator.itemgetter(1))
 >>> it
[('Captain Planet', '000'), ('Enid Blyton', '005'), ('Enid Blyton also', 
'006'), ('James Bond', '007')]

In Python 2.3 you have two choices:
- Use the decorate - sort - undecorate idiom (aka Schwartzian Transform)
- Define your own comparison function

I'm out of time for explaining right now, maybe someone else can pick up 
the thread or I'll come back to it later...

Kent

Liam Clarke wrote:
> Kia ora,
> 
> Just exploring the wonderful world of dictionaries, and I'm trying to
> comprehend the sort() method, because as I found, dictionaries are not
> sorted.
> 
> So, I've got a dictionary, miscPeople= {'James Bond' : '007' , 'Enid
> Blyton' : '005' , 'Enid Blyton also' : '006' , 'Captain Planet' :
> '000' }
> 
> (Okay, so that's a sample dictionary)
> 
> So, if I wanted to sort by alphabetical key i.e. (first names here)
> 
> for name, numerical in miscPeopledict.sorted() (pseudo code):
>    print name, numerical
> 
> Captain Planet 000
> Enid Blyton 005
> Enid Blyton also 006
> James Bond 007
> 
> or conversely, to sort by value in some manner
> 
> for name, numerical in miscPeopledict.sorted.reverse() 
> 
> James Bond 007
> Enid Blyton also 006
> Enid Blyton 005
> Captain Planet 000
> 
> That's just an example of output. I'm not too hung up on the output at
> the moment, just sort().
> 
> It says to use a comparison function...
> 
> And I have no idea how to work that.
> 
> ??? Very confused. I've tried searching for info, there's some very
> complex ways out there to sort data, usually with proper first names
> (Schwartzian transformations...), but I can't seem to find the simple.
> 
> Could anyone post some useful links for my education?
> 
> Much appreciated.
> 
> Liam Clarke
> 
> 


More information about the Tutor mailing list