问题描述
我的索引倒数了.它由我的单词词典和出现这些术语的文档过帐列表组成.我只想按字母顺序对字典进行排序.这就是现在的样子(示例):
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 onthis 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])]
我尝试了什么:
self.index = sorted(self.index)
print self.index
print self.index['all']
第一个打印呼叫提供了一个完美的单词排序列表,但是如果我尝试获取单词"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
推荐答案
在字典上调用sorted()
只会按排序顺序返回键的列表.字典本身没有固有的顺序,您无法对它们进行排序.
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.
由于您将sorted()
的输出重新分配回了self.index
,因此现在您失去了对原始defaultdict
的引用.
Because you re-assigned the output of sorted()
back to self.index
, you've now lost your reference to the original defaultdict
.
这篇关于如何在defaultdict(list)中按键(字母顺序)排序以获取反向索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!