I need to have a collection, in which I insert items such as [1,'b42b00d6-76c8-4d68-b22e-ff4653bb01c8'].
It needs to be ordered by the first element, but indexable by the second.
The following is the best I could come up with. It has two flaws:
- It can't take multiple items with the same key, since it's a dictionary.
- It can't properly delete items from the list.
My attempt:
from rbtree import rbtree
class Item(object):
def __init__(self, value, id):
self.value = value
self.id = id
item1 = Item(1,'b42b00d6-76c8-4d68-b22e-ff4653bb01c8')
item2 = Item(2,'60eda62f-f05d-4134-9e92-9bb9a1f52daf')
item3 = Item(2,'77d9a028-bd4b-4634-b230-234f88ff010a')
item4 = Item(3,'7e7118cd-7145-41c8-8413-79670bdc81dc')
myList = rbtree()
myList[item2.value] = item2
myList[item1.value] = item1
myList[item3.value] = item3
myList[item4.value] = item4
# Correctly ordered by the first element
# But it's missing item2.
for k,v in myList.iteritems():
print "%s %s" % (v.value, v.id)
# But I also need to index by the second element.
# So:
listIndexedBySecondElement = {}
listIndexedBySecondElement[item1.id] = item1
listIndexedBySecondElement[item2.id] = item2
listIndexedBySecondElement[item3.id] = item3
listIndexedBySecondElement[item4.id] = item4
item = listIndexedBySecondElement['7e7118cd-7145-41c8-8413-79670bdc81dc']
print item.value # correctly prints 3
# Now I need to delete an element.
del listIndexedBySecondElement['b42b00d6-76c8-4d68-b22e-ff4653bb01c8']
# But I also need to delete it from myList. How do I do that?