Search code examples
pythonsortingdefaultdict

How to order by key (alphabetically) in defaultdict(list) for an inverted index


I have an inverted index. It consists of my word dictionary and the posting list of documents in which the terms appear. What I simply want is to sort my dictionary alphabetically. This is how it looks right now (example):

self.index = 
defaultdict(<type 'list'>, {
'all': [['d03', array('I', [32L, 40L)], ['d07', array('I', [32L, 40L, 47L])], ['d05', array('I', [32L, 40L, 47L])]],
'just': [['d03', array('I', [11L])], ['d07', array('I', [11L])], ['d05', array('I', [11L])], ['d08', array('I', [11L])]])
'collect': [['d04', array('I', [24L])]]
'occurring': [['d03', array('I', [34L])], ['d07', array('I', [34L])]

...and so on this is how it should look like after sorting:

'all': [['d03', array('I', [32L, 40L)], ['d07', array('I', [32L, 40L, 47L])], ['d05', array('I', [32L, 40L, 47L])]],
'collect': [['d04', array('I', [24L])]]
'just': [['d03', array('I', [11L])], ['d07', array('I', [11L])], ['d05', array('I', [11L])], ['d08', array('I', [11L])]])
'occurring': [['d03', array('I', [34L])], ['d07', array('I', [34L])]

what i tried:

self.index = sorted(self.index)
print self.index
print self.index['all']

the first print call delivers a perfect sorted list of words but if I try to get the connected postinglist for the word 'all', I receive this error message:

TypeError: list indices must be integers, not str

Solution

  • Calling sorted() on a dictionary returns just a list of the keys in sorted order. Dictionaries themselves have no inherent order, you cannot sort those.

    Because you re-assigned the output of sorted() back to self.index, you've now lost your reference to the original defaultdict.