如何更新不断变化的类变量



我正在尝试自动更新后缀节点类中的结束变量。 我的意思是我在下面的代码中创建了一个 SuffixNode,并将 endIndex.end 分配为 SuffixNode 的结束值。 然后我将 endIndex.end 更新为 2。 但是,当我在更新 endIndex.end 值后打印出 (self.root.end( 时,后缀节点中的结束值存储仍然显示 1 而不是显示更新的 2。

谁能为我提供关于我应该如何修改代码的建议,以便当我更新 endIndex.end 时,SuffixNode 中的结束值存储也会自动更新。

谢谢

下面是代码

类结束索引: definit(self, endIndexValue(: self.end = endIndexValue

class ActivePoint:
def __init__(self, activeLength, activeNode, activeEdge, suffixCount):
self.activeLength = activeLength
self.activeNode = activeNode
self.activeEdge = activeEdge
self.suffixCount = suffixCount

class SuffixNode:
def __init__(self, start, end, index = None):
# Create a list storing all the possible english alphabet
# This will be used to store the starting characte
self.children = [None] * 87
# The pointer to the other node via suffix link
self.suffixLink = None
# The index of the start and end of the substring
self.index = index
self.start = start
self.end = end

class SuffixTree:
def __init__(self):
# Initiate Active Point Values the End Index Value
activePoints = ActivePoint(0, 0, 0, 0)
endIndex = EndIndex(0)
print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, endIndex.end))
endIndex.end = 1
print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, endIndex.end))
self.root = SuffixNode(0, endIndex.end)
print(self.root.end)

endIndex.end = endIndex.end + 1
print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, endIndex.end))
print(self.root.end)

suffixTree = SuffixTree()

您根本不需要在代码中创建endIndex。这是您需要做出的唯一改变。所以,你的SuffixTree应该是这样的:

class SuffixTree:
def __init__(self):
# Initiate Active Point Values the End Index Value
activePoints = ActivePoint(0, 0, 0, 0)
self.root = SuffixNode(0, 0)  #<---- defing root here with 0 as a start value for end
print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, self.root.end)) #<---- use self.root.end instead
self.root.end = 1  #<---- use self.root.end instead of endIndex.end
print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, self.root.end)) #<---- use self.root.end instead
print(self.root.end)

self.root.end += 1  #<---- use self.root.end instead of endIndex.end
print("AL: %d | AN: %d | AE: %d | ASC: %d | End: %d" % (activePoints.activeLength, activePoints.activeNode, activePoints.activeEdge, activePoints.suffixCount, self.root.end)) #<---- use self.root.end
print(self.root.end)

当你实例化SuffixNodeself.root时,self.root.end被赋endIndex.end当前值self.root.end不是对endIndex.end的引用,它只是endIndex.end赋值中包含的整数的副本。现在,如果您分配了self.root = endEndex,当endIndex.end更改时,将显示self.root.end更改。

最新更新