Proposed implementation for an Ordered Dictionary
Steven D'Aprano
steve at pearwood.info
Sat Feb 28 11:27:53 EST 2009
Colin J. Williams wrote:
> Sometimes, it's useful to be able to
> obtain the data in the sorted sequence.
>
> You might consider adding functionality
> like:
>
> def seqItems(self):
> '''To return the items, sorted
> by key. '''
> return [self[k] for k in
> self.seqKeys()]
Amazingly, the Python time-machine provides such functionality! Using just a
handful of pre-existing pieces, you can do this:
sorted(mydict.items())
sorted(mydict.keys())
[mydict[x] for x in sorted(mydict.keys)]
and any other sequence you might need. That's the beauty of a general
purpose programming language. You don't need everything to be a built-in
*wink*
--
Steven
More information about the Python-list
mailing list