[Python-ideas] `OrderedDict.sort`
M.-A. Lemburg
mal at egenix.com
Tue Sep 24 17:49:12 CEST 2013
On 24.09.2013 17:23, Ram Rachum wrote:
> Ethan, you've misunderstood my message and given a correct objection to an
> argument I did not make.
>
> I did not argue against ordering by insertion order on init. I agree with
> that decision. I disagree with defining the entire class as an insertion
> ordering class and refusing to allow users to reorder it as they wish after
> it's created.
The overhead introduced by completely recreating the internal
data structure after the sort is just as high as creating a
new OrderedDict, so I don't understand why you don't like about:
from collections import OrderedDict
o = OrderedDict(((3,4), (5,4), (1,2)))
p = OrderedDict(sorted(o.iteritems()))
This even allows you to keep the original insert order should
you need it again. If you don't need this, you can just use:
o = dict(((3,4), (5,4), (1,2)))
p = OrderedDict(sorted(o.iteritems()))
which is also faster than first creating an OrderedDict and
then recreating it with sorted entries.
Put those two lines into a function and you have:
def SortedOrderedDict(*args, **kws):
o = dict(*args, **kws)
return OrderedDict(sorted(o.iteritems()))
p = SortedOrderedDict(((3,4), (5,4), (1,2)))
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Source (#1, Sep 24 2013)
>>> Python Projects, Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________
2013-09-11: Released eGenix PyRun 1.3.0 ... http://egenix.com/go49
2013-09-28: PyDDF Sprint ... 4 days to go
2013-10-14: PyCon DE 2013, Cologne, Germany ... 20 days to go
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
More information about the Python-ideas
mailing list