sorteddict PEP proposal [started off as orderedict]

Mark Summerfield mark at qtrac.eu
Tue Sep 25 06:34:23 EDT 2007


On 2007-09-25, Andrew Durdin wrote:
> On 9/25/07, Mark Summerfield <m.n.summerfield at googlemail.com> wrote:
> >     Since the sorteddict's data is always kept in key order, indexes
> >     (integer offsets) into the sorteddict make sense.  Five additional
> >     methods are proposed to take advantage of this:
> >
> >     key(index : int) -> value
> >
> >     item(index : int) -> (key, value)
> >
> >     value(index : int) -> key
> >
> >     set_value(index : int, value)
> >
> >     delete(index : int)
>
> But what about using non-sequential integer keys (something I do quite
> often)?
>
> e.g. sorteddict({1:'a', 3:'b': 5:'c', 99:'d'})[3]  should return 'b', not
> 'd'.
>
> Andrew

The sorteddict really does work in key order, so:

    d = sorteddict({1:'a', 3:'b', 5:'c', 99:'d'})
    d.items()
    [(1, 'a'), (3, 'b'), (5, 'c'), (99, 'd')]

If you do d[3] you will get 'd' since that is the 4th sequential item.
If you want to get the item with _key_ 3 then use value():

    d.value(3)
    'd'


PS In my previous reply I got my example wrong a second time: should have 
been:
    for item in itemsByDate.values():...



-- 
Mark Summerfield, Qtrac Ltd., www.qtrac.eu




More information about the Python-list mailing list