How to order by key (alphabetically) in defaultdic

2019-08-29 12:44发布

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

3条回答
唯我独甜
2楼-- · 2019-08-29 13:08

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.

查看更多
Root(大扎)
3楼-- · 2019-08-29 13:10

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.

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-29 13:15

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/

查看更多
登录 后发表回答