我正在倒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帮助将不胜感激。
你的'全球'什么都不做;你不会分配名称'nestedDict'。 –
循环中的前两行是写'currentPosition = int(tokens [i])'的一种非常复杂的方式。 –