Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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 哈夫曼代码的帮助_Python - Fatal编程技术网

Python 哈夫曼代码的帮助

Python 哈夫曼代码的帮助,python,Python,好的,我有我的哈夫曼班,看起来很好。但是,当我尝试生成二叉树时,我不断收到一条错误消息,当我的函数名为encoding时,该消息说“未定义全局名称编码”。有人能告诉我为什么吗?下面是我的代码。多谢各位 def get_encoding(self): # Huff is a previously defined function that generates the tree node=self.Huff() dic={} if n

好的,我有我的哈夫曼班,看起来很好。但是,当我尝试生成二叉树时,我不断收到一条错误消息,当我的函数名为encoding时,该消息说“未定义全局名称编码”。有人能告诉我为什么吗?下面是我的代码。多谢各位

def get_encoding(self):
        # Huff is a previously defined function that generates the tree 
        node=self.Huff()
        dic={}
        if node.char:
            if not self:
                dic[node.char]='0'
            else:
                dic[node.char]=self
        else:
               encoding(self+'0',node.left)
               encoding(self+'1',node.right)

函数名不是
encoding
,而是
get\u encoding

看起来您正在尝试递归调用get\u encoding,但签名不同,因为定义不包含两个参数。您尝试执行
self+'0'
self+'1'

时可能也有问题,因为输入错误。即使我有get_编码,它仍然会给我同样的错误message@steff,这就是为什么剪贴是一种很好的提问方式的原因之一。:@厕所。谢谢你的回复。无论如何,我也注意到了,我也做了更正,但出于一些未知的原因,它仍然给出了同样愚蠢的错误消息@Johnthat“全局名称get_编码没有定义”@john我实际上重写了整个过程。但是我不能让它自己调用get_编码函数