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.










share|improve this question



















  • 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 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














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.










share|improve this question



















  • 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 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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












  • 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 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







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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo