Auto sort in Dictionaries???
nperkins7 at home.com
Mon Jul 23 04:28:27 EDT 2001
"phillip" <phillip at transwitch.co.za> wrote in message
news:74cc9702.0107222235.78bd9d35 at posting.google.com...
> I put 'name,values' into a dictionary.
> But when I get the keys and display the values they come out in a
> different order that the order I inserted them in.
> Is there a way maintain the oringinal order?
This cookbook recipe deals with a similar idea...
...but not exactly keeping keys in the order in which they were inserted.
In Python 2.2a1 you can sub-class a dictionary to make it behave this way.
Here's a first stab at it...
""" dictionary which returns keys in the order of insertion """
self.n = 0
self.n += 1
ks = dictionary.keys(self)
return [ key for n,key in ks ]
...I think you would also have to implement items(), and probably a few
others as well to get them all working right. I would like to see someone
else post a better version of this code.
More information about the Python-list