Q: sort's key and cmp parameters

Raymond Hettinger python at rcn.com
Sat Oct 3 02:38:14 CEST 2009


[Paul Rubin]
> Yes, think of sorting tree structures where you have to recursively
> compare them til you find an unequal pair of nodes.  

I'm not sure what you mean by this.  What are the semantics of
sorting a tree?  Can you outline an example of tree that
could be sorted easily with a cmp function but not a key function?

t = [[9,6],[7,1]],[[2,5],[4,3]]   # inputs
t.sort(mycmp)                     # what would the cmp function be?
print t                           # what would the output be


Raymond



More information about the Python-list mailing list