Appending a self constructed class in priority que gives TypeError: '<' not supported between instances of 'str' and 'Node'
Appending a self constructed class in priority que gives TypeError: '<' not supported between instances of 'str' and 'Node'
import sys
from queue import PriorityQueue
class Node(object):
def __init__(self, data = None):
self.value = data
self.right = None
self.left = None
class Huffman():
def __init__(self, data):
self.data = data
def encoding(self):
my_dict = {}
for i in self.data:
my_dict[i] = my_dict.get(i, 0)+1
q = PriorityQueue()
for i, j in my_dict.items():
q.put((j,i))
while q.qsize() >= 2:
val_1 = q.get()
val_2 = q.get()
new_node_1 = Node(val_1[0]+val_2[0])
if isinstance(val_1[1], str):
new_node_1.left = val_1[1]
else:
new_node_1.left = val_1[1]
if isinstance(val_2[1], str):
new_node_1.right = val_2[1]
else:
new_node_1.right = val_2[1]
q.put((new_node_1.value, new_node_1))
在这里,我在while循环中遇到错误,经过几次操作后它无法使用方法q.get(),或q.put()。
例如,Huffman('AADCIDCVUSHDUSHUSAHDIADHIAD').encoding()
我不想更改整个代码,而只是想更改优先级,以便它仅根据第一个元素(整数)对条目进行优先级排序。
你是对的,优先级队列不知道如何比较 str 和节点。您必须覆盖节点中的比较器函数 class 让优先级队列知道如何比较两种不同的类型。
示例(根据需要更改比较器):
class Node(object):
def __init__(self, data=None):
self.value = data
self.right = None
self.left = None
def __lt__(self, obj):
"""self < obj."""
return self.value < obj
def __le__(self, obj):
"""self <= obj."""
return self.value <= obj
def __eq__(self, obj):
"""self == obj."""
return self.value == obj
def __ne__(self, obj):
"""self != obj."""
return self.value != obj
def __gt__(self, obj):
"""self > obj."""
return self.value > obj
def __ge__(self, obj):
"""self >= obj."""
return self.value >= obj
import sys
from queue import PriorityQueue
class Node(object):
def __init__(self, data = None):
self.value = data
self.right = None
self.left = None
class Huffman():
def __init__(self, data):
self.data = data
def encoding(self):
my_dict = {}
for i in self.data:
my_dict[i] = my_dict.get(i, 0)+1
q = PriorityQueue()
for i, j in my_dict.items():
q.put((j,i))
while q.qsize() >= 2:
val_1 = q.get()
val_2 = q.get()
new_node_1 = Node(val_1[0]+val_2[0])
if isinstance(val_1[1], str):
new_node_1.left = val_1[1]
else:
new_node_1.left = val_1[1]
if isinstance(val_2[1], str):
new_node_1.right = val_2[1]
else:
new_node_1.right = val_2[1]
q.put((new_node_1.value, new_node_1))
在这里,我在while循环中遇到错误,经过几次操作后它无法使用方法q.get(),或q.put()。
例如,Huffman('AADCIDCVUSHDUSHUSAHDIADHIAD').encoding()
我不想更改整个代码,而只是想更改优先级,以便它仅根据第一个元素(整数)对条目进行优先级排序。
你是对的,优先级队列不知道如何比较 str 和节点。您必须覆盖节点中的比较器函数 class 让优先级队列知道如何比较两种不同的类型。
示例(根据需要更改比较器):
class Node(object):
def __init__(self, data=None):
self.value = data
self.right = None
self.left = None
def __lt__(self, obj):
"""self < obj."""
return self.value < obj
def __le__(self, obj):
"""self <= obj."""
return self.value <= obj
def __eq__(self, obj):
"""self == obj."""
return self.value == obj
def __ne__(self, obj):
"""self != obj."""
return self.value != obj
def __gt__(self, obj):
"""self > obj."""
return self.value > obj
def __ge__(self, obj):
"""self >= obj."""
return self.value >= obj