每次查询时是否需要读取索引文件?

2024-10-04 11:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python实现对django的搜索

在我的查询类中,我改变索引的位置值,这反过来改变实例数据,如果每次都使用查询对象的同一个实例,它会改变临时字典,如果第二次执行相同的查询,它会产生其他结果。 但是,通过创建一个新的查询索引实例(即每次都从文件中获取索引),它可以正常工作,我想知道是否可以创建一个只读字典,或者是否可以使实例不改变字典数据

def getPosIntersection(self, docID, terms):
    for ID in docID:
        for term in terms:
            for actualTerm in term:
                if ID == actualTerm[0]:
                    self.posIntersectionSet.append(actualTerm[1])

        print "DOCID -> %s" % (docID)        
        print "posIntersectionSet -> %s" % (self.posIntersectionSet)

        intersection = self.posIntersectionSet[0]
        for index, pos in enumerate(self.posIntersectionSet):
            if index > 0:
                for secondIndex, val in enumerate(pos):
                    pos[secondIndex] = pos[secondIndex] - index

            intersection = set(intersection).intersection(pos)

        if intersection:
            if ID not in self.presentDocs:
                print "Pos intersection -> %s" % intersection
                self.presentDocs.append(ID)  

在第15行,我改变了数据,这反过来又改变了我传递的另一个类变量的Dictionary实例,如何使Dictionary只读并只改变它的实例

找到答案: 我实现了一个只读字典,而且值是完整的,谢谢


Tags: 数据实例inposselfidforindex