我有一个图表和一个节点类。在方法中,我为图形创建了一个新节点;然后我调用该节点的一个属性:'女儿'。我收到错误:AttributeError: 'str' object has no attribute 'daughters'
,以回应第for daughter in currentNode.daughters
行。
为什么有一个字符串作为currentNode而不是Node对象?
class Graph:
def __init__(self):
self.node_list = {}
self.number = 0
self.names = []
def node(self, node_name):
if node_name in self.node_list:
return self.node_list[node_name
else:
self.node_list[node_name] = Node()
return self.node_list[node_name]
....
class Node:
def __init__(self):
self.daughters = []
...
def PrefixTrieConstruction(patterns):
trie = Graph()
trie.node('root')
for pattern in patterns:
currentNode = trie.node('root')
for symbol in pattern:
for daughter in currentNode.daughters:
if daughter.label == symbol:
currentNode = daughter
break
else:
node_name = trie.name_node()
trie.node(node_name)
trie.edge(currentNode, node_name, symbol)
currentNode = node_name
return trie