首页 > 解决方案 > 如何更新不断变化的类变量

问题描述

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

谁能给我一个关于如何修改代码的建议,这样当我更新 endIndex.end 时,存储在 SuffixNode 中的最终值也会自动更新。

谢谢

下面是代码

类 EndIndex: def init (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()

标签: python

解决方案


您根本不需要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)

推荐阅读