python - 如何更新不断变化的类变量
问题描述
我正在尝试自动更新 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()
解决方案
您根本不需要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)
推荐阅读
- python - python SocketIO-client 和 nodejs socket.io 服务器命名空间不匹配
- datestamp - 很长的日期戳格式
- html - 如何让我的整个网站在移动设备上响应?
- mysql - my.cnf 根据配置优化
- c# - .NET Standard:程序集的清单定义与程序集引用不匹配
- c++ - 如何避免 R 值悬空参考
- grammar - 在 Xtext 中为可选数组维度定义语法
- angular - FormBuilder 验证无法正常工作
- intellij-idea - 为什么 Intellij Idea 无法生成 maven 插件?
- c# - 根据参数过滤对象列表