indexed dictionaries?
Kevin Jacobs
jacobs at darwin.epbi.cwru.edu
Mon Apr 10 15:28:30 EDT 2000
David Goodger <dgoodger at bigfoot.com> wrote:
> on 2000-04-09 22:30, Michal Wallace (sabren) (sabren at manifestation.com)
> wrote:
>> I'm looking for a cross between a dictionary and a list,
>> such that keys() always returns keys in the order in which they were
>> added, and that instance[0] refers to the first key...
> ...
>> Anyone got something like this?
> I have implemented a list/dictionary hybrid in my SGF Parser Library (class
> Node in http://gotools.sourceforge.net/sgflib/ ). It doesn't do ordered
> keys(), but it does do indexing by integer (note that in this library, no
> numeric dictionary keys will ever be used). It may give you a few ideas.
> Please send me a copy of your final implementation of indexed dictionaries;
> I'd be interested to see what you come up with.
Here is my rough 3 minute attempt.
~Kevin
# WARNING: Untested code
class SequenceDict(UserDict):
def __init__(self):
UserDict.__init__(self)
self._seq = []
def __setitem__(self,key,value):
if not self.data.has_key(key):
self._seq.append(key)
self.data[key]=value
def __delitem__(self,key):
if self.data.has_key(key):
self._seq.remove(key)
del self.data[key]
def seq_keys(self):
return self._seq
def seq_items(self):
return map(None, self._seq, self.seq_values())
def seq_values(self):
return map(operator.getitem, [self]*len(self._seq), self._seq)
--
-----------> Kevin Jacobs <-----------|-------> (216) 778-8487 <--------
S.A.G.E. Project Technical Coordinator | Department of Epidemiology
& System Administrator | & Biostatistics
Internet E-mail: jacobs at darwin.cwru.edu | Case Western Reserve University
----------------------------------------------------------------------------
More information about the Python-list
mailing list