2017-09-24 86 views
0

我正在倒index.For为此,我从一个文件的file.Each值取值在形式上:Python的多层次默认字典

DOCUMENT_ID“\ t'term_Id” \ t'pos_1' \ t'pos_2 ... '\ t'pos_n

这是一个正向索引representation.I想将其转换成倒排索引其内容应当类似于

term_Id' \ T'“DOC_ID:POS1,POS2 ... posn“”doc_Id:pos1,pos2 ... posn“

为此,我使用列表类型的默认字典。这是我的乐趣ction:

nestedDict = defaultdict(lambda:defaultdict(list)) 

def getInfo(line): 
    global nestedDict 
    tokens = re.split(r'\t+',line) 
    docInfo = int(tokens[0]) #Set document Id 
    termId = int(tokens[1]) #Set Term Id 
    currentPosition = int(tokens[2]) 
    nestedDict[str(termId)][str(docInfo)] = str(currentPosition)   
    if len(tokens) > 3 : 
     for i in range(3,len(tokens)): 
      position = int(tokens[i])-currentPosition 
      currentPosition = currentPosition + position 
      nestedDict[str(termId)][str(docInfo)].append(currentPosition) 

这是给我一个错误:力量有没有方法.append。 我是新来的python.Any帮助将不胜感激。

+0

你的'全球'什么都不做;你不会分配名称'nestedDict'。 –

+1

循环中的前两行是写'currentPosition = int(tokens [i])'的一种非常复杂的方式。 –

回答

0

嵌套的defaultdict使nestedDict[...][...]成为list,但是随后给它分配了一个字符串。无论如何,我认为你不需要这个任务:为什么不让循环处理所有的位置?

+0

你能举个例子吗? –

+0

@MuhammadRaghib:你的意思是'对于代币中的t [2:]:...'? –

+0

我的意思是我应该怎样做才能更好。我很困惑。 –