I am getting File SyntaxError: invalid syntax for line 13 self = self.trieDict[word[0]]
I am getting File SyntaxError: invalid syntax for line 13 self = self.trieDict[word[0]]
我正在编写代码以将单词插入到 trie 数据结构中,然后搜索单词。我收到行 self = self.trieDict[word[0]] 的无效语法错误(插入函数中的第 3 行)
#Trie data structure
class TrieNode():
trieDict = {}
isComplete = False
def __init__(self, dic, isComplete):
self.trieDict = dic
self.isComplete = isComplete
#self is the root node
def insert(self, word):
while len(word) != 0 and self is not None:
if word[0] in self.trieDict:
self = self.trieDict[word[0]]
word = word[1:]
else:
child = self.TrieNode({}, False)
self.trieDict[word[0]] = child
self = child
word = word[1:]
self.isComplete = True
def search(self, word):
while len(word) != 0 and self is not None:
if word[0] in self.trieDict:
word = word[1:]
self = self.trieDict[word[0]]
else:
return False
return self.isComplete
当我从您的代码中复制以下行时
self = self.trieDict[word[0]]
第二个 self
前面有一个无法识别的符号,这会导致您的语法错误。 (好像是 Unicode 0013)直接删除它或在新行上重写该行并删除有问题的行。
旁注,在方法中分配给 self
通常不是一个好主意,因为它指向您正在执行该方法的实例。虽然在语法上没有错,但肯定会导致 reader.
混淆
这里是修改后的代码(用于向trie中插入节点并在trie中查找节点:
class TrieNode():
trieDict = {}
isComplete = False
def __init__(self, dic, isComplete):
self.trieDict = dic
self.isComplete = isComplete
#self is the root node
def insert(self, word):
current = self
while len(word) != 0 and current is not None:
if word[0] in current.trieDict:
current = current.trieDict[word[0]]
word = word[1:]
else:
child = TrieNode({}, False)
current.trieDict[word[0]] = child
current = child
word = word[1:]
current.isComplete = True
def search(self, word):
current = self
while len(word) != 0 and current is not None:
if word[0] in current.trieDict:
current = current.trieDict[word[0]]
word = word[1:]
else:
return False
return current.isComplete
def test():
node = TrieNode({}, False)
node.insert('cat')
node.insert('car')
node.insert('pod')
print(node.search('car'))
print(node.search('ccar'))
print(node.search('pod'))
print(node.search('pode'))
test()
我正在编写代码以将单词插入到 trie 数据结构中,然后搜索单词。我收到行 self = self.trieDict[word[0]] 的无效语法错误(插入函数中的第 3 行)
#Trie data structure
class TrieNode():
trieDict = {}
isComplete = False
def __init__(self, dic, isComplete):
self.trieDict = dic
self.isComplete = isComplete
#self is the root node
def insert(self, word):
while len(word) != 0 and self is not None:
if word[0] in self.trieDict:
self = self.trieDict[word[0]]
word = word[1:]
else:
child = self.TrieNode({}, False)
self.trieDict[word[0]] = child
self = child
word = word[1:]
self.isComplete = True
def search(self, word):
while len(word) != 0 and self is not None:
if word[0] in self.trieDict:
word = word[1:]
self = self.trieDict[word[0]]
else:
return False
return self.isComplete
当我从您的代码中复制以下行时
self = self.trieDict[word[0]]
第二个 self
前面有一个无法识别的符号,这会导致您的语法错误。 (好像是 Unicode 0013)直接删除它或在新行上重写该行并删除有问题的行。
旁注,在方法中分配给 self
通常不是一个好主意,因为它指向您正在执行该方法的实例。虽然在语法上没有错,但肯定会导致 reader.
这里是修改后的代码(用于向trie中插入节点并在trie中查找节点:
class TrieNode():
trieDict = {}
isComplete = False
def __init__(self, dic, isComplete):
self.trieDict = dic
self.isComplete = isComplete
#self is the root node
def insert(self, word):
current = self
while len(word) != 0 and current is not None:
if word[0] in current.trieDict:
current = current.trieDict[word[0]]
word = word[1:]
else:
child = TrieNode({}, False)
current.trieDict[word[0]] = child
current = child
word = word[1:]
current.isComplete = True
def search(self, word):
current = self
while len(word) != 0 and current is not None:
if word[0] in current.trieDict:
current = current.trieDict[word[0]]
word = word[1:]
else:
return False
return current.isComplete
def test():
node = TrieNode({}, False)
node.insert('cat')
node.insert('car')
node.insert('pod')
print(node.search('car'))
print(node.search('ccar'))
print(node.search('pod'))
print(node.search('pode'))
test()