Ordered dictionaries?

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Thu Dec 13 19:38:21 EST 2001


My Red-Black Tree module at
http://newcenturycomputers.net/projects/rbtree.html
implements an ordered dictionary, RBDict.

----- Original Message -----
From: "David Brady" <daves_spam_dodging_account at yahoo.com>
To: "Python" <python-list at python.org>
Sent: Thursday, December 13, 2001 3:45 PM
Subject: Ordered dictionaries?


> Hello,
>
> One last Perl module that I need to replace to
> complete my Python toolkit is a custom module that let
> me create a dictionary whose keys were settable and
> retrievable in a particular order.  Essentially, this
> let us have a list whose ordered elements were also
> accessible by name.
>
> Has anyone done this?  If not, what class operators
> would I need to override to create my own class that
> does this?
>
> Example of how it would work, assuming OrderedDict was
> a working class of what I wanted:
>
> >>> od = OrderedDict()
> >>> od['Alice'] = 'Anderson'
> >>> od['Bob'] = 'Bradley'
> >>> od.append('Christiansen')
> >>> od.['Dave'] = 'Dobson'
> >>> od.keys()
> ['Alice', 'Bob', 2, 'Dave']
> >>> od.values()
> ['Anderson', 'Bradley', 'Christiansen', 'Dobson']
> >>> od[3]
> 'Dobson'
> >>> od[7] = 'Johnson'
> # Notice assignment does not trigger IndexError
> >>> od.values()
> ['Anderson', 'Bradley', 'Christiansen', 'Dobson']
> >>> len(od)
> 8
> >>> for i in range(len(od)): print od[i]
> Anderson
> Bradley
> Christiansen
> Dobson
> None
> None
> None
> Johnson
>
> ...etc.  Notice that this is making the python list
> act more like a Perl list, which may not be desirable.
>  Also, I don't know if the None object "exists" in
> terms of taking up space in the list, my thinking is
> that it shouldn't; that trying to access an undefined
> element should create the None object and return it
> rather than padding the list with statically-created
> None objects.
>
> Anyway, if anyone has already done this, I'd love to
> see it; if not, some tips about where to start would
> be wonderful.  Is there a canonical list of all the
> __functions__ a Python object can have, when they are
> called and why, and what they must do if implemented
> by a user?
>
> Thank you,
>
> -dB
>
> =====
> David Brady
> daves_spam_dodging_account at yahoo.com
> I'm feeling very surreal today... or *AM* I?
>
> __________________________________________________
> Do You Yahoo!?
> Check out Yahoo! Shopping and Yahoo! Auctions for all of
> your unique holiday gifts! Buy at http://shopping.yahoo.com
> or bid at http://auctions.yahoo.com
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list