good name for a dict/list hybrid
Andrew Dalke
adalke at mindspring.com
Mon Jan 6 18:45:49 EST 2003
I have a class which I'm calling DictList. Its used for an ordered
dictionary which can have multiple keys, though in almost all cases
it only has single keys. It is ordered, so I want away to get an
entry by position in the insertion order, which are integers.
This means I can't use integers as keys, which is not a problem.
Here's what it looks like
>>> dl = DictList.DictList()
>>> dl["Name"] = "Andrew"
>>> dl["PubDate"] = "1975 Jun"
>>> dl["Lang"] = "English"
>>> print dl.keys()
['Lang', 'Name', 'PubDate']
>>> print dl[0]
('Name', 'Andrew')
>>> dl["Name"] = "Andrew Dalke" # Duplicate entry
>>> dl["Name"] # Only gets the first entry
'Andrew'
>>> dl.getall("Name") # Gets all "Name" entries
['Andrew', 'Andrew Dalke']
>>> dl.allitems()
>>> list(dl.allitems())
[('Name', 'Andrew'), ('PubDate', '1975 Jun'), ('Lang', 'English'),
('Name', 'Andrew Dalke')]
>>> del dl["PubDate"]
>>> list(dl.allitems())
[('Name', 'Andrew'), ('Lang', 'English'), ('Name', 'Andrew Dalke')]
>>> dl.keys()
['Lang', 'Name']
>>>
I can't figure out what to call this thing. I chose "DictList"
because it's a hybrid between a dictionary and a list, but that's
a bad name since it can be confused with a dictionary of lists, or
a list of dictionaries.
This sort of data structure must be common. Eg, headers in email
act like this. (rfc822.py provides no hints for a name.)
What should I use?
Code will be posted after I get a good name. :)
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list