Return lambda Node < Node
up vote
0
down vote
favorite
I'm receiving this compiler error when putting a Node onto a min-priority queue.
TypeError: '<' not supported between instances of 'Node' and 'Node'
Here is where it fails
from queue import PriorityQueue
def huffman(text):
A = frequency(text) # returns a dictionary (frequency, character),...
q = PriorityQueue()
heap = build_min_heap(A) # builds min heap
while heap:
temp = pop_heap(heap) # format (frequency, character)
----> q.put(Node(temp[1],temp[0],None,None))
# Node(character, frequency, left child, right child)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
--->if lefti < heap_size and key(A[lefti]) < key(A[i]):
/anaconda3/lib/python3.6/queue.py in put(self, item, block, timeout)
141 raise Full
142 self.not_full.wait(remaining)
--> 143 self._put(item)
144 self.unfinished_tasks += 1
145 self.not_empty.notify()
/anaconda3/lib/python3.6/queue.py in _put(self, item)
225
226 def _put(self, item):
--> 227 heappush(self.queue, item)
228
229 def _get(self):
I found a workaround by adding an "lt" function to the Node class.
def __lt__(self, other):
return self.frequency < other.frequency
However, is there another way to fix this using lambda expressions or modifying the min-priority queue in some way?
Maybe a key function perhaps? I understand what key(value) is doing but I don't know how would I interpret it as a Node. I tried something like the following, but it didn't work.
def key(item):
if instanceof(item, Node):
return item.frequency
Also to note that the heap functions also process int values and other types. This is way later in the code where I'm passing Nodes to the heap/queue.
Note: The Node is sorted by frequency.
Here is my Node class
class Node():
def __init__(self, character=None, frequency=None, left=None, right=None):
self.character = character
self.frequency = frequency
self.left = left
self.right = right
def isLeaf(self):
return self.left is None and self.right is None
def __lt__(self, other):
return self.frequency < other.frequency
def __repr__(self):
return 'Node(, , , )'.format(self.character,
self.frequency,
self.left, self.right)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
if lefti < heap_size and key(A[lefti]) < key(A[i]):
smallesti = lefti
else:
smallesti = i
if righti < heap_size and key(A[righti]) < key(A[smallesti]):
smallesti = righti
if smallesti != i:
A[i], A[smallesti] = A[smallesti], A[i]
min_heapify(A, smallesti, key)
def build_min_heap(A, key=lambda a: a):
for i in range(len(A) // 2 - 1, -1, -1):
swaps = min_heapify(A, i, key)
return A
A final word, I understand that "lt" works in the Node class, but I'm trying to figure out another solution that doesn't involve modifying the Node class because other sites have comments that say to use a lambda expression (e.g. The key function should return the frequency stored in the argument to key, which should be a Node.; do we need to write the key function? Yes. It can be a simple lambda expression.) but they are vague in how to accomplish that.
python heap nodes priority-queue
add a comment |
up vote
0
down vote
favorite
I'm receiving this compiler error when putting a Node onto a min-priority queue.
TypeError: '<' not supported between instances of 'Node' and 'Node'
Here is where it fails
from queue import PriorityQueue
def huffman(text):
A = frequency(text) # returns a dictionary (frequency, character),...
q = PriorityQueue()
heap = build_min_heap(A) # builds min heap
while heap:
temp = pop_heap(heap) # format (frequency, character)
----> q.put(Node(temp[1],temp[0],None,None))
# Node(character, frequency, left child, right child)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
--->if lefti < heap_size and key(A[lefti]) < key(A[i]):
/anaconda3/lib/python3.6/queue.py in put(self, item, block, timeout)
141 raise Full
142 self.not_full.wait(remaining)
--> 143 self._put(item)
144 self.unfinished_tasks += 1
145 self.not_empty.notify()
/anaconda3/lib/python3.6/queue.py in _put(self, item)
225
226 def _put(self, item):
--> 227 heappush(self.queue, item)
228
229 def _get(self):
I found a workaround by adding an "lt" function to the Node class.
def __lt__(self, other):
return self.frequency < other.frequency
However, is there another way to fix this using lambda expressions or modifying the min-priority queue in some way?
Maybe a key function perhaps? I understand what key(value) is doing but I don't know how would I interpret it as a Node. I tried something like the following, but it didn't work.
def key(item):
if instanceof(item, Node):
return item.frequency
Also to note that the heap functions also process int values and other types. This is way later in the code where I'm passing Nodes to the heap/queue.
Note: The Node is sorted by frequency.
Here is my Node class
class Node():
def __init__(self, character=None, frequency=None, left=None, right=None):
self.character = character
self.frequency = frequency
self.left = left
self.right = right
def isLeaf(self):
return self.left is None and self.right is None
def __lt__(self, other):
return self.frequency < other.frequency
def __repr__(self):
return 'Node(, , , )'.format(self.character,
self.frequency,
self.left, self.right)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
if lefti < heap_size and key(A[lefti]) < key(A[i]):
smallesti = lefti
else:
smallesti = i
if righti < heap_size and key(A[righti]) < key(A[smallesti]):
smallesti = righti
if smallesti != i:
A[i], A[smallesti] = A[smallesti], A[i]
min_heapify(A, smallesti, key)
def build_min_heap(A, key=lambda a: a):
for i in range(len(A) // 2 - 1, -1, -1):
swaps = min_heapify(A, i, key)
return A
A final word, I understand that "lt" works in the Node class, but I'm trying to figure out another solution that doesn't involve modifying the Node class because other sites have comments that say to use a lambda expression (e.g. The key function should return the frequency stored in the argument to key, which should be a Node.; do we need to write the key function? Yes. It can be a simple lambda expression.) but they are vague in how to accomplish that.
python heap nodes priority-queue
6
That's not a workaround, that's literally "adding support for '<' between instances of 'Node' and 'Node'". Why do you want to do it any other way (especially using lambda expressions)?
– jedwards
Nov 9 at 20:47
5
Since a priority queue needs to understand sorting order of the objects in it, it seems like supporting the order in the node class itself is the most sensible thing to do.
– Mark Meyer
Nov 9 at 20:48
Why are you using bothbuild_min_heap
andqueue.PriorityQueue
? What even isbuild_min_heap
? It kind of sounds likebuild_min_heap
already handles node ordering the way you want.
– user2357112
Nov 9 at 20:50
Sorry, I added the min heap functions. I thought it wasn't relevant but your comment convinced me it was.
– N. Colostate
Nov 9 at 20:52
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm receiving this compiler error when putting a Node onto a min-priority queue.
TypeError: '<' not supported between instances of 'Node' and 'Node'
Here is where it fails
from queue import PriorityQueue
def huffman(text):
A = frequency(text) # returns a dictionary (frequency, character),...
q = PriorityQueue()
heap = build_min_heap(A) # builds min heap
while heap:
temp = pop_heap(heap) # format (frequency, character)
----> q.put(Node(temp[1],temp[0],None,None))
# Node(character, frequency, left child, right child)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
--->if lefti < heap_size and key(A[lefti]) < key(A[i]):
/anaconda3/lib/python3.6/queue.py in put(self, item, block, timeout)
141 raise Full
142 self.not_full.wait(remaining)
--> 143 self._put(item)
144 self.unfinished_tasks += 1
145 self.not_empty.notify()
/anaconda3/lib/python3.6/queue.py in _put(self, item)
225
226 def _put(self, item):
--> 227 heappush(self.queue, item)
228
229 def _get(self):
I found a workaround by adding an "lt" function to the Node class.
def __lt__(self, other):
return self.frequency < other.frequency
However, is there another way to fix this using lambda expressions or modifying the min-priority queue in some way?
Maybe a key function perhaps? I understand what key(value) is doing but I don't know how would I interpret it as a Node. I tried something like the following, but it didn't work.
def key(item):
if instanceof(item, Node):
return item.frequency
Also to note that the heap functions also process int values and other types. This is way later in the code where I'm passing Nodes to the heap/queue.
Note: The Node is sorted by frequency.
Here is my Node class
class Node():
def __init__(self, character=None, frequency=None, left=None, right=None):
self.character = character
self.frequency = frequency
self.left = left
self.right = right
def isLeaf(self):
return self.left is None and self.right is None
def __lt__(self, other):
return self.frequency < other.frequency
def __repr__(self):
return 'Node(, , , )'.format(self.character,
self.frequency,
self.left, self.right)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
if lefti < heap_size and key(A[lefti]) < key(A[i]):
smallesti = lefti
else:
smallesti = i
if righti < heap_size and key(A[righti]) < key(A[smallesti]):
smallesti = righti
if smallesti != i:
A[i], A[smallesti] = A[smallesti], A[i]
min_heapify(A, smallesti, key)
def build_min_heap(A, key=lambda a: a):
for i in range(len(A) // 2 - 1, -1, -1):
swaps = min_heapify(A, i, key)
return A
A final word, I understand that "lt" works in the Node class, but I'm trying to figure out another solution that doesn't involve modifying the Node class because other sites have comments that say to use a lambda expression (e.g. The key function should return the frequency stored in the argument to key, which should be a Node.; do we need to write the key function? Yes. It can be a simple lambda expression.) but they are vague in how to accomplish that.
python heap nodes priority-queue
I'm receiving this compiler error when putting a Node onto a min-priority queue.
TypeError: '<' not supported between instances of 'Node' and 'Node'
Here is where it fails
from queue import PriorityQueue
def huffman(text):
A = frequency(text) # returns a dictionary (frequency, character),...
q = PriorityQueue()
heap = build_min_heap(A) # builds min heap
while heap:
temp = pop_heap(heap) # format (frequency, character)
----> q.put(Node(temp[1],temp[0],None,None))
# Node(character, frequency, left child, right child)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
--->if lefti < heap_size and key(A[lefti]) < key(A[i]):
/anaconda3/lib/python3.6/queue.py in put(self, item, block, timeout)
141 raise Full
142 self.not_full.wait(remaining)
--> 143 self._put(item)
144 self.unfinished_tasks += 1
145 self.not_empty.notify()
/anaconda3/lib/python3.6/queue.py in _put(self, item)
225
226 def _put(self, item):
--> 227 heappush(self.queue, item)
228
229 def _get(self):
I found a workaround by adding an "lt" function to the Node class.
def __lt__(self, other):
return self.frequency < other.frequency
However, is there another way to fix this using lambda expressions or modifying the min-priority queue in some way?
Maybe a key function perhaps? I understand what key(value) is doing but I don't know how would I interpret it as a Node. I tried something like the following, but it didn't work.
def key(item):
if instanceof(item, Node):
return item.frequency
Also to note that the heap functions also process int values and other types. This is way later in the code where I'm passing Nodes to the heap/queue.
Note: The Node is sorted by frequency.
Here is my Node class
class Node():
def __init__(self, character=None, frequency=None, left=None, right=None):
self.character = character
self.frequency = frequency
self.left = left
self.right = right
def isLeaf(self):
return self.left is None and self.right is None
def __lt__(self, other):
return self.frequency < other.frequency
def __repr__(self):
return 'Node(, , , )'.format(self.character,
self.frequency,
self.left, self.right)
def min_heapify(A, i, key=lambda a: a):
lefti = 2 * i + 1
righti = 2 * i + 2
heap_size = len(A)
if lefti < heap_size and key(A[lefti]) < key(A[i]):
smallesti = lefti
else:
smallesti = i
if righti < heap_size and key(A[righti]) < key(A[smallesti]):
smallesti = righti
if smallesti != i:
A[i], A[smallesti] = A[smallesti], A[i]
min_heapify(A, smallesti, key)
def build_min_heap(A, key=lambda a: a):
for i in range(len(A) // 2 - 1, -1, -1):
swaps = min_heapify(A, i, key)
return A
A final word, I understand that "lt" works in the Node class, but I'm trying to figure out another solution that doesn't involve modifying the Node class because other sites have comments that say to use a lambda expression (e.g. The key function should return the frequency stored in the argument to key, which should be a Node.; do we need to write the key function? Yes. It can be a simple lambda expression.) but they are vague in how to accomplish that.
python heap nodes priority-queue
python heap nodes priority-queue
edited Nov 9 at 21:09
asked Nov 9 at 20:44
N. Colostate
306
306
6
That's not a workaround, that's literally "adding support for '<' between instances of 'Node' and 'Node'". Why do you want to do it any other way (especially using lambda expressions)?
– jedwards
Nov 9 at 20:47
5
Since a priority queue needs to understand sorting order of the objects in it, it seems like supporting the order in the node class itself is the most sensible thing to do.
– Mark Meyer
Nov 9 at 20:48
Why are you using bothbuild_min_heap
andqueue.PriorityQueue
? What even isbuild_min_heap
? It kind of sounds likebuild_min_heap
already handles node ordering the way you want.
– user2357112
Nov 9 at 20:50
Sorry, I added the min heap functions. I thought it wasn't relevant but your comment convinced me it was.
– N. Colostate
Nov 9 at 20:52
add a comment |
6
That's not a workaround, that's literally "adding support for '<' between instances of 'Node' and 'Node'". Why do you want to do it any other way (especially using lambda expressions)?
– jedwards
Nov 9 at 20:47
5
Since a priority queue needs to understand sorting order of the objects in it, it seems like supporting the order in the node class itself is the most sensible thing to do.
– Mark Meyer
Nov 9 at 20:48
Why are you using bothbuild_min_heap
andqueue.PriorityQueue
? What even isbuild_min_heap
? It kind of sounds likebuild_min_heap
already handles node ordering the way you want.
– user2357112
Nov 9 at 20:50
Sorry, I added the min heap functions. I thought it wasn't relevant but your comment convinced me it was.
– N. Colostate
Nov 9 at 20:52
6
6
That's not a workaround, that's literally "adding support for '<' between instances of 'Node' and 'Node'". Why do you want to do it any other way (especially using lambda expressions)?
– jedwards
Nov 9 at 20:47
That's not a workaround, that's literally "adding support for '<' between instances of 'Node' and 'Node'". Why do you want to do it any other way (especially using lambda expressions)?
– jedwards
Nov 9 at 20:47
5
5
Since a priority queue needs to understand sorting order of the objects in it, it seems like supporting the order in the node class itself is the most sensible thing to do.
– Mark Meyer
Nov 9 at 20:48
Since a priority queue needs to understand sorting order of the objects in it, it seems like supporting the order in the node class itself is the most sensible thing to do.
– Mark Meyer
Nov 9 at 20:48
Why are you using both
build_min_heap
and queue.PriorityQueue
? What even is build_min_heap
? It kind of sounds like build_min_heap
already handles node ordering the way you want.– user2357112
Nov 9 at 20:50
Why are you using both
build_min_heap
and queue.PriorityQueue
? What even is build_min_heap
? It kind of sounds like build_min_heap
already handles node ordering the way you want.– user2357112
Nov 9 at 20:50
Sorry, I added the min heap functions. I thought it wasn't relevant but your comment convinced me it was.
– N. Colostate
Nov 9 at 20:52
Sorry, I added the min heap functions. I thought it wasn't relevant but your comment convinced me it was.
– N. Colostate
Nov 9 at 20:52
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233022%2freturn-lambda-node-node%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
That's not a workaround, that's literally "adding support for '<' between instances of 'Node' and 'Node'". Why do you want to do it any other way (especially using lambda expressions)?
– jedwards
Nov 9 at 20:47
5
Since a priority queue needs to understand sorting order of the objects in it, it seems like supporting the order in the node class itself is the most sensible thing to do.
– Mark Meyer
Nov 9 at 20:48
Why are you using both
build_min_heap
andqueue.PriorityQueue
? What even isbuild_min_heap
? It kind of sounds likebuild_min_heap
already handles node ordering the way you want.– user2357112
Nov 9 at 20:50
Sorry, I added the min heap functions. I thought it wasn't relevant but your comment convinced me it was.
– N. Colostate
Nov 9 at 20:52