[Tutor] Keeping Dictonary Entries Ordered
spir
denis.spir at free.fr
Thu Feb 12 14:38:54 CET 2009
Le Thu, 12 Feb 2009 09:25:22 -0000,
"Alan Gauld" <alan.gauld at btinternet.com> a écrit :
> > config_names = {"start_time : '18:00:00', 'gray_scale' : True,
> > "long": 120.00}
> >
> > If I iterate over it, the entries will appear in any order, as
> > opposed to
> > what I see above. However, in the config file, I'd like to keep them
> > in the order above.
>
> Thats tricky! Dictionaries are not sorted and do not retain insertion
> order.
Had to do something similar.
I just subtyped dict to add a separate list a keys. Initiated with the possible initial arguments, then updated by setattr. Very few code lines. Then Ordict had a simplissim method to return values (and pairs) the keylist's order.
The only issue was rather a semantic one: should a value change keep the keylist unchanged or move the key to the last position?
Denis
------
la vida e estranya
More information about the Tutor
mailing list