Dalke Scientific Software: More science. Less time. Products

[Download MultiDict.py]

This implements two types of dictionary-like objects where there can be more than one entry with the same key. One is OrderedMultiDict, which preserves the order of all entries across all keys. The other is UnorderedMultidict, which only preserves the order of entries for the same key.

Example:
>>> import MultiDict
>>> od = MultiDict.OrderedMultiDict()
>>> od["Name"] = "Andrew"; od["Color"] = "Green"
>>> od["Name"] = "Karen"; od["Color"] = "Brown"
>>> od["Name"]
'Karen'
>>> od.getall("Name")
['Andrew', 'Karen']
>>> for k, v in od.allitems():
...     print "%r == %r", (k, v)
...
'Name' == 'Andrew'
'Color' == 'Green'
'Name' == 'Karen'
'Color' == 'Brown'
>>> ud = MultDict.UnorderedMultiDict(od)
>>> for k, v in ud.allitems():
...     print "%r == %r", (k, v)
...
'Name' == 'Andrew'
'Name' == 'Karen'
'Color' == 'Green'
'Color' == 'Brown'
>>> 

MultiDict has been placed in the public domain.



Copyright © 2001-2013 Andrew Dalke Scientific AB