Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在获取文件语法错误:第13行self=self.trieDict[word[0]的语法无效_Python_Trie - Fatal编程技术网

Python 我正在获取文件语法错误:第13行self=self.trieDict[word[0]的语法无效

Python 我正在获取文件语法错误:第13行self=self.trieDict[word[0]的语法无效,python,trie,Python,Trie,我正在编写代码,将单词插入trie数据结构,然后搜索单词。我收到self= self.trieDict[word[0]]行的无效语法错误(插入函数的第三行) 当我从您的代码中复制以下行时 self=self.trieDict[word[0]] 导致语法错误的第二个self前面有一个无法识别的符号。(似乎是Unicode 0013)只需删除它或在新行上重写该行,然后删除有问题的行 另一方面,在方法中指定给self通常不是一个好主意,因为它指向执行该方法的实例。虽然没有语法错误,但它肯定会给读者造

我正在编写代码,将单词插入trie数据结构,然后搜索单词。我收到self= self.trieDict[word[0]]行的无效语法错误(插入函数的第三行)


当我从您的代码中复制以下行时

self=self.trieDict[word[0]]

导致语法错误的第二个
self
前面有一个无法识别的符号。(似乎是Unicode 0013)只需删除它或在新行上重写该行,然后删除有问题的行


另一方面,在方法中指定给
self
通常不是一个好主意,因为它指向执行该方法的实例。虽然没有语法错误,但它肯定会给读者造成混淆。

以下是更正后的代码(用于将节点插入到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()

显示完整的错误回溯!虽然这行代码的功能非常奇怪,但语法中没有错误。很难看到,但代码实际上是
self=\x13self.trieDict[word[0]]
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()