私は、ファイルのfile.Each値から値を取っています。この目的index.For反転作ってい形である:Pythonのマルチレベルのデフォルトの辞書
DOCUMENT_ID「\ t'term_Idは」\ t'pos_1' \ t'pos_2 ... '\ t'pos_n
これは
term_Idのようになりますrepresentation.Iは転置インデックスに変換したい前方指標である' \ 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)
これは私にエラーを与えています:Strにはメソッドがありません。 私はpython.Anyヘルプに新しいです非常に高く評価される新しいです。
あなたの 'global'は何もしません。あなたは 'nestedDict'という名前に割り当てていません。 –
ループの最初の2行は、 'currentPosition = int(tokens [i])'を書くための本当に複雑な方法です。 –