Python 如何在列表中分隔节点的子节点

Python 如何在列表中分隔节点的子节点,python,python-3.x,Python,Python 3.x,我正在研究一个trie数据结构,其中每个节点都是一个列表,其中的条目包含一个值和对应于该值的子节点的引用,但是列表中的引用似乎没有相互分离- 例如,如果“the”和“sin”都在trie中,那么my CONTAINS()函数将为“she”返回true,因为“s”在第一级,“h”在第二级,“e”在第三级,即使这些字符应该在不同的分支中。我读过python引用,但不明白为什么会发生这种情况 class triepair: value = None next = None d

我正在研究一个trie数据结构,其中每个节点都是一个列表,其中的条目包含一个值和对应于该值的子节点的引用,但是列表中的引用似乎没有相互分离- 例如,如果“the”和“sin”都在trie中,那么my CONTAINS()函数将为“she”返回true,因为“s”在第一级,“h”在第二级,“e”在第三级,即使这些字符应该在不同的分支中。我读过python引用,但不明白为什么会发生这种情况

class triepair:
    value = None
    next = None

    def __init__(self, value=None, next=None):
        self.value = value
        self.next = next

    def NEXT(self,next):
        self.next = next

    def GETNEXT(self):
        return self.next

class trienode:
    nodespace = []

    def __int__(self,nodespace=[]):
        self.nodespace = nodespace

    def APPEND(self,value):
        newnext = trienode()
        newpair = triepair(value,newnext)
        self.nodespace.append(newpair)

    def NEXT(self, value):
        for triepair in self.nodespace:
            if triepair.value == value:
                return triepair.GETNEXT()

        print("ERROR: value not found")
        return None

    def CONTAINS(self, value):
        for triepair in self.nodespace:
            if triepair.value == value:
                return True

        return False

    def INSERT(self, word):
        c = word[:1]
        rest = word[1:]

        if self.CONTAINS(c):
            if len(rest) > 0:
                nextnode = self.NEXT(c)
                nextnode.INSERT(rest)

        else:
            self.APPEND(c)
            if len(rest) > 0:
                nextnode = self.NEXT(c)
                nextnode.INSERT(rest)


    def TRACE(self, word):
        c = word[:1]
        rest = word[1:]
        if self.CONTAINS(c):
            print "found character ",c
            if self.NEXT(c) is not None and len(rest) > 0:
                self.NEXT(c).TRACE(rest)
            else:
                print "trace complete"

    def HASWORD(self, word):
        c = word[:1]
        rest = word[1:]

        if self.CONTAINS(c):
            #print str(self)," contains ",c
            if len(rest) > 0:
                return self.NEXT(c).HASWORD(rest)
            else:
                return True

        else:
            return False

class trie:
    root = trienode()

    def __init__(self):
        self.root = trienode()

    def INSERT(self,word):
        self.root.INSERT(word)

    def TRACE(self,word):
        if self.root is not None:
            self.root.TRACE(word)
        else:
            print("null trie")

    def CONTAINS(self, word):
        return self.root.HASWORD(word)

哈哈,这花了我一段时间调试,但我找到了

更改此项:

class trienode:
    nodespace = []

    def __int__(self,nodespace=[]):
        self.nodespace = nodespace
为此:

class trienode:
    nodespace = []

    def __init__(self):
        self.nodespace = []
首先,
\uuuuu int\uuuu
不是一件事,但这只是问题的一部分

在不知道确切原因的完整Python细节的情况下,根
nodespace
被重新使用

因此,当您在
APPEND()
中创建一个新节点时,它实际上不会发生,并且在新的“子节点”中会得到相同的
nodespace

结果,一切都是平淡的。所有
value->trinode
对都处于同一级别,并且trinode引用始终指向同一事物


无论如何,通过上面的更改,您可以确保从一个新的空
节点空间开始,因此
APPEND()
.NEXT().CONTAINS()
等将更像您期望的那样工作。

哈哈,我花了一段时间进行调试,但我发现了它

更改此项:

class trienode:
    nodespace = []

    def __int__(self,nodespace=[]):
        self.nodespace = nodespace
为此:

class trienode:
    nodespace = []

    def __init__(self):
        self.nodespace = []
首先,
\uuuuu int\uuuu
不是一件事,但这只是问题的一部分

在不知道确切原因的完整Python细节的情况下,根
nodespace
被重新使用

因此,当您在
APPEND()
中创建一个新节点时,它实际上不会发生,并且在新的“子节点”中会得到相同的
nodespace

结果,一切都是平淡的。所有
value->trinode
对都处于同一级别,并且trinode引用始终指向同一事物


无论如何,通过上述更改,您可以确保从一个新的空
节点空间开始,因此
APPEND()
.NEXT().CONTAINS()
等将更像您期望的那样工作。

类内定义的任何变量(在init或其他方法之外)都属于类,而不是实例。是否确实要为这些类使用全局
节点空间
和全局
下一个
变量,而不是实例属性?为什么要使用SHOUTY方法名称?您应该遵循。是否可以添加重现问题的行,并澄清在类内部(在init或其他方法之外)定义的预期输出和实际输出变量属于类而不是实例。是否确实要为这些类使用全局
节点空间
和全局
下一个
变量,而不是实例属性?为什么要使用SHOUTY方法名称?您应该遵循。您可以添加重现问题的行,并澄清您的预期输出和实际输出吗