an indexed list - how to do it in python

dmost at magna.com.au dmost at magna.com.au
Sat Aug 12 12:23:34 EDT 2000


I want to create, in Python, an object that mixes the behavior of a
sequence and a mapping. The primary behavior should be that of a
mutable sequence, or list, but the objects in the list should also be
accessible by a key value.

In my mind, this object would be defined something like this:

Class IndexedSequence:
    def __init__(self):
        self.data = []
        self.keys = []
        self.index = {}


At some point in time, this data structure might look like this:
self.data = ["nurk", "bazar"]
self.keys = ["fred", "jill"]
seld.index = {"fred":0, "jill":1}

Operations that append to this data structure operate just fine.
The problem is that there is no way to eliminate the renumbering
required by insertion or deletion.

I could rebuild the index on the next index access operation after
insertion or deletion, but this seems overly stupid to me.

Given an object and a list to which it belongs, how can I find its
position in a list in O(1) operations.



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list