【发布时间】:2017-06-06 02:19:31
【问题描述】:
我有一个变量索引,其结构如下:
{ ‘web’: [ [1, [0, 2]], [2, [2]] ], ‘retrieval’: [ [1, [1]] ], ‘search’: [ [1, [3]], [2, [0]] ], ‘information’: [ [1, [4]] ], ‘engine’: [ [2, [1]] ], ‘ranking’: [ [2, [3]] ] }
我需要把它写成以下格式的文件
term|docID1:pos1,pos2;docID2:pos3,pos4,pos5;…
因此,帖子列表对 ‘web’: [ [1, [0, 2]], [2, [2]] ] 将保存为:web|1:0,2;2:2
这是我正在使用的代码,
def writeIndexToFile(self):
'''write the inverted index to the file'''
f=open("indexFile.dat", 'w')
for term in self.index.items():
postinglist=[]
for p in self.index[term]:
docID=p[0]
positions=p[1]
postinglist.append(':'.join([str(docID) ,','.join(map(str,positions))]))
print >> f, ''.join((term,'|',';'.join(postinglist)))
f.close()
我收到以下错误:
for p in self.index[term]:
TypeError: unhashable type: 'list'
我正在使用 python 3.4。
【问题讨论】:
-
调试 101:查看问题中涉及的变量的内容(
term,对于初学者)
标签: python python-3.x