How to order by key (alphabetically) in defaultdic

2019-08-29 12:34发布

问题:

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

回答1:

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.



回答2:

I do not believe dictionaries can be sorted in the sense you are referring to. If you want to view the dictionary sorted you can try the following:

sorted(self.index.items())

Note however that the result is not a dictionary - its just a list of (key, value) tuples which would be associated with each other in the original dictionary.



回答3:

I read this yesterday and i think it might be just what you are looking for. Its a Binary Heap implementation for Python dictionaries. It puts out its items in sorted order if you call a for on it.

http://code.activestate.com/recipes/117228-priority-dictionary/