Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 在defaultdict中使用构造函数有效吗_Python_Python 3.x - Fatal编程技术网

Python 在defaultdict中使用构造函数有效吗

Python 在defaultdict中使用构造函数有效吗,python,python-3.x,Python,Python 3.x,我试图看看是否可以在defaultdict中使用构造函数,但我无法运行代码并得到递归错误。只是想知道是否可能: from collections import defaultdict class TrieNode: def __init__(self, char): self.children = defaultdict(TrieNode(char)) self.is_word = False a = TrieNode('b') 在构造函数中使用d

我试图看看是否可以在defaultdict中使用构造函数,但我无法运行代码并得到递归错误。只是想知道是否可能:

from collections import defaultdict

class TrieNode:
    def __init__(self, char):
        self.children = defaultdict(TrieNode(char))
        self.is_word = False

 a = TrieNode('b')

在构造函数中使用
defaultdict
没有什么错。问题是您需要向它传递一个函数,当您添加新键时它将调用该函数。在创建字典时,您当前正在调用该函数。因此,您不断无限地调用
trinode('b')

您需要用以下方式来称呼它:

self.children = defaultdict(TrieNode)
from collections import defaultdict

class TrieNode:
    def __init__(self):
        self.children = defaultdict(TrieNode)
        self.is_word = False
        self.val = ''

    def add(self, word):
        self.val= word[0]
        if (len(word) == 1):
            self.is_word = True
        else:
            self.children[word[0]].add(word[1:])

    def words(self):
        if self.is_word:
            yield self.val
        for letter, node in self.children.items():
            yield from (letter + child for child in node.words())
然后,当您在
children
中引用未知键时,它将为您调用
trinode()
。但是,这意味着您不想在构造函数中使用额外的参数

这可能没问题,因为您通常向trie添加单词,并且需要通过同一个节点添加许多单词。一种选择是采取如下措施:

self.children = defaultdict(TrieNode)
from collections import defaultdict

class TrieNode:
    def __init__(self):
        self.children = defaultdict(TrieNode)
        self.is_word = False
        self.val = ''

    def add(self, word):
        self.val= word[0]
        if (len(word) == 1):
            self.is_word = True
        else:
            self.children[word[0]].add(word[1:])

    def words(self):
        if self.is_word:
            yield self.val
        for letter, node in self.children.items():
            yield from (letter + child for child in node.words())
然后,您可以向其添加单词,它将在默认词典中生成三元组:

node = TrieNode()
node.add("dog")
node.add("catnip")
node.add("cats")
node.add("cat")
node.add("crunch")

node.children['c'].children

> defaultdict(__main__.TrieNode,
        {'a': <__main__.TrieNode at 0x179c70048>,
         'r': <__main__.TrieNode at 0x179c70eb8>})

不要在这里输入代码。复制粘贴您运行的代码。如果这样做,您就不会犯错误,比如缺少
\uuuu init\uuuu
调用的参数。defaultdict是偶然的。假设您的
\uuuu init\uuuu
方法应该有一个
char
参数,您的问题是
\uuuuuu init\uuuu
三元组
正在尝试创建另一个
三元组
,其中一个必须尝试创建另一个,而另一个将尝试创建另一个,这是一个无限递归。除了无限递归之外:需要一个可调用的
default\u工厂
,返回默认对象。您正在传递一个预先构造的对象。很抱歉,我没有正确复制它。我确实运行了代码,这就是为什么我得到了一个递归错误。