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 '<';在';str';和';节点';?_Python_Python 3.x - Fatal编程技术网

Python '<';在';str';和';节点';?

Python '<';在';str';和';节点';?,python,python-3.x,Python,Python 3.x,我正在尝试实现哈夫曼编码算法,但由于优先级队列的比较,不断出现此错误。我的代码如下: class Node(object): def __init__(self, left=None, right=None): self.left = left self.right = right def children(self): return(self.left, self.right) def __lt__(self,other)

我正在尝试实现哈夫曼编码算法,但由于优先级队列的比较,不断出现此错误。我的代码如下:

class Node(object):
    def __init__(self, left=None, right=None):
        self.left = left
        self.right = right
    def children(self):
        return(self.left, self.right)
    def __lt__(self,other):
        return 0

def create_tree(count):
    print(count)
    priority = queue.PriorityQueue()
    for value in count:
        priority.put(value)
    while priority.qsize() > 1:
        one, two = priority.get(), priority.get()
        node = Node(one, two)
        priority.put((one[0]+two[0], node))
    return priority.get()
我曾多次尝试在HuffmanNode类中使用_ult_uuu,但最终总是无法将“str”与“Node”、“int”或“tuple”进行比较。我想知道是否有可能做到这一点,任何建议将不胜感激

编辑:create_tree的另一个计数是元组列表,如下所示: [(1,'a'),(1,'b'),(2,'c')]

应该产生错误的代码:[(1113),(1107),(11998),(1120),(1106),(11118),(1108),(11212),(11000),(187),(170),(1010),(184),(2117),(299),(2119),(2102),(2110),(3114),(4115),(4116),(4103),(5104),(5104),(5104),(5104),(5104),(5104),(5101),(7105),(8111),(1832)]


还意识到我更改了代码,使其使用ascii值而不是字符串,但无论是“int”还是“str”,我都会收到相同的错误。这是直接从文档中删除的:

首先检索值最低的条目(值最低的条目
返回的是已排序的(列表(条目))[0])
。典型模式 for entries是一个元组,格式为:
(优先级号,数据)

如果数据元素不可比较,则可以将数据包装在 类,该类忽略数据项并仅比较优先级 号码

他们给出的示例代码是:

from dataclasses import dataclass, field
from typing import Any

@dataclass(order=True)
class PrioritizedItem:
    priority: int
    item: Any=field(compare=False)
使用您为
count=[(1,'a'),(1,'b'),(2,'c')]提供的示例

反之亦然:

def __lt__(self, other):
    return False

def __gt__(self, other):
    return True

或者更复杂的东西?

当您实现
\uu lt\uuu()
时,您为
节点
创建比较,但不是
节点

为此,我们需要反转操作:

这些方法没有交换的参数版本(当左参数不支持该操作而右参数支持该操作时使用);相反,
是彼此的反映,
是彼此的反映,
是各自的反映

换句话说,当评估
B
时,即
B.\uu lt\uuuuuuuuuuuuuuuuuuuu(A)
B
没有定义合适的
\uuuuu lt\uuuuuuuuuuuuuuuu()
方法,而是使用
A.\uu gt\uuuuuuuuuuuuu(B)

要解决此问题,请将以下内容添加到
节点
类中:

def __gt__(self,other):
    return 0 # or something else intelligent

我无法重现任何错误:
create_-tree([(1,'a'),(1,'b'),(2,'c')])
工作正常,
n<1
。你能提供产生错误的代码吗?只是添加了它。实际上,我刚刚意识到我修改了代码,所以它不使用字符串,我可以把它改回去,但我认为这两种方式都没有什么不同。无论是“int”还是“str”,我都会得到相同的错误。如果优先级元素相同,那么
(2,Node(left=(1,'a'),right=(1,'b'))
应该如何使用
(2,'c')
排序队列
查找数据元素。要获得错误,需要删除
\lt\uuuu()
节点中的
方法
@SuperShoot,这就是为什么我想知道它是否可能。删除_ult__;()并不能修复错误。
def __lt__(self, other):
    return False

def __gt__(self, other):
    return True
def __gt__(self,other):
    return 0 # or something else intelligent