我在python中实现 django 的搜索。
在我的Query类中,我改变了索引的位置值,这反过来改变了实例数据。如果我每次都使用相同的查询对象实例,它会改变临时字典,如果我做相同的查询,第二次它导致其他东西。 但是,通过拥有查询索引的新实例,即每次从文件中获取索引,它工作正常,我想知道我是否可以创建只读字典或使实例不改变字典数据。
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只读,只改变它的实例。
答案发现: 我实现了一个只读字典,值完好无损,谢谢