How to invert the order of elements in a heapq heap with object comparison functions?
First of all, I read this SO question but it actually doesn't include my desired approach. In addition, negating the actual values is not applicable for my use case.
Heapq Docs: https://docs.python.org/3.6/library/heapq.html
Assume I have a list of dataclass objects in my heap. Only the a
property determines the order of objects.
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a < other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=2, b=4)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
Now I want to have an inverted order. Therefore, I changed the line return self.a < other.a
to return self.a > other.a
. The result:
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
print(heapq.heappop(l)) # C(a=2, b=4)
The desired result should be one of the four solutions:
C(a=9, b=109) C(a=9, b=4) C(a=9, b=109) C(a=9, b=4)
C(a=9, b=4) C(a=9, b=109) C(a=9, b=4) C(a=9, b=109)
C(a=2, b=1) C(a=2, b=1) C(a=2, b=4) C(a=2, b=4)
C(a=2, b=4) C(a=2, b=4) C(a=2, b=1) C(a=2, b=1)
Probably, not all pairs of objects are compared by heapq
that would explain the strange order. However, is it still possible to get an inverted order?
Do I have to provide more object comparison methods?
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
If you have an completely other approach, do not hesitate!
python python-3.x heap python-3.6
add a comment |
First of all, I read this SO question but it actually doesn't include my desired approach. In addition, negating the actual values is not applicable for my use case.
Heapq Docs: https://docs.python.org/3.6/library/heapq.html
Assume I have a list of dataclass objects in my heap. Only the a
property determines the order of objects.
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a < other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=2, b=4)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
Now I want to have an inverted order. Therefore, I changed the line return self.a < other.a
to return self.a > other.a
. The result:
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
print(heapq.heappop(l)) # C(a=2, b=4)
The desired result should be one of the four solutions:
C(a=9, b=109) C(a=9, b=4) C(a=9, b=109) C(a=9, b=4)
C(a=9, b=4) C(a=9, b=109) C(a=9, b=4) C(a=9, b=109)
C(a=2, b=1) C(a=2, b=1) C(a=2, b=4) C(a=2, b=4)
C(a=2, b=4) C(a=2, b=4) C(a=2, b=1) C(a=2, b=1)
Probably, not all pairs of objects are compared by heapq
that would explain the strange order. However, is it still possible to get an inverted order?
Do I have to provide more object comparison methods?
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
If you have an completely other approach, do not hesitate!
python python-3.x heap python-3.6
add a comment |
First of all, I read this SO question but it actually doesn't include my desired approach. In addition, negating the actual values is not applicable for my use case.
Heapq Docs: https://docs.python.org/3.6/library/heapq.html
Assume I have a list of dataclass objects in my heap. Only the a
property determines the order of objects.
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a < other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=2, b=4)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
Now I want to have an inverted order. Therefore, I changed the line return self.a < other.a
to return self.a > other.a
. The result:
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
print(heapq.heappop(l)) # C(a=2, b=4)
The desired result should be one of the four solutions:
C(a=9, b=109) C(a=9, b=4) C(a=9, b=109) C(a=9, b=4)
C(a=9, b=4) C(a=9, b=109) C(a=9, b=4) C(a=9, b=109)
C(a=2, b=1) C(a=2, b=1) C(a=2, b=4) C(a=2, b=4)
C(a=2, b=4) C(a=2, b=4) C(a=2, b=1) C(a=2, b=1)
Probably, not all pairs of objects are compared by heapq
that would explain the strange order. However, is it still possible to get an inverted order?
Do I have to provide more object comparison methods?
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
If you have an completely other approach, do not hesitate!
python python-3.x heap python-3.6
First of all, I read this SO question but it actually doesn't include my desired approach. In addition, negating the actual values is not applicable for my use case.
Heapq Docs: https://docs.python.org/3.6/library/heapq.html
Assume I have a list of dataclass objects in my heap. Only the a
property determines the order of objects.
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a < other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=2, b=4)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
Now I want to have an inverted order. Therefore, I changed the line return self.a < other.a
to return self.a > other.a
. The result:
import heapq
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
print(heapq.heappop(l)) # C(a=2, b=1)
print(heapq.heappop(l)) # C(a=9, b=109)
print(heapq.heappop(l)) # C(a=9, b=4)
print(heapq.heappop(l)) # C(a=2, b=4)
The desired result should be one of the four solutions:
C(a=9, b=109) C(a=9, b=4) C(a=9, b=109) C(a=9, b=4)
C(a=9, b=4) C(a=9, b=109) C(a=9, b=4) C(a=9, b=109)
C(a=2, b=1) C(a=2, b=1) C(a=2, b=4) C(a=2, b=4)
C(a=2, b=4) C(a=2, b=4) C(a=2, b=1) C(a=2, b=1)
Probably, not all pairs of objects are compared by heapq
that would explain the strange order. However, is it still possible to get an inverted order?
Do I have to provide more object comparison methods?
object.__lt__(self, other)
object.__le__(self, other)
object.__eq__(self, other)
object.__ne__(self, other)
object.__gt__(self, other)
object.__ge__(self, other)
If you have an completely other approach, do not hesitate!
python python-3.x heap python-3.6
python python-3.x heap python-3.6
edited Nov 23 '18 at 16:29
d4rty
asked Nov 23 '18 at 16:23
d4rtyd4rty
1,36321333
1,36321333
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to make l
into a heap using heapify
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
prints
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
1
Because your example list happens to satisfy the heap invariant when<
isn't reversed, but isn't a valid heap when<
is reversed. The Theory section of theheapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.
– Patrick Haugh
Nov 23 '18 at 16:47
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53450056%2fhow-to-invert-the-order-of-elements-in-a-heapq-heap-with-object-comparison-funct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to make l
into a heap using heapify
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
prints
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
1
Because your example list happens to satisfy the heap invariant when<
isn't reversed, but isn't a valid heap when<
is reversed. The Theory section of theheapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.
– Patrick Haugh
Nov 23 '18 at 16:47
add a comment |
You need to make l
into a heap using heapify
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
prints
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
1
Because your example list happens to satisfy the heap invariant when<
isn't reversed, but isn't a valid heap when<
is reversed. The Theory section of theheapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.
– Patrick Haugh
Nov 23 '18 at 16:47
add a comment |
You need to make l
into a heap using heapify
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
prints
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)
You need to make l
into a heap using heapify
from heapq import heapify, heappop
from dataclasses import dataclass
@dataclass
class C:
a: int
b: int
def __lt__(self, other):
return self.a > other.a
l=[C(2,1),C(9,109),C(2,4),C(9,4)]
heapify(l)
while l:
print(heappop(l))
prints
C(a=9, b=4)
C(a=9, b=109)
C(a=2, b=1)
C(a=2, b=4)
edited Nov 23 '18 at 16:40
Jon Clements♦
99.5k19174219
99.5k19174219
answered Nov 23 '18 at 16:30
Patrick HaughPatrick Haugh
29k92747
29k92747
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
1
Because your example list happens to satisfy the heap invariant when<
isn't reversed, but isn't a valid heap when<
is reversed. The Theory section of theheapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.
– Patrick Haugh
Nov 23 '18 at 16:47
add a comment |
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
1
Because your example list happens to satisfy the heap invariant when<
isn't reversed, but isn't a valid heap when<
is reversed. The Theory section of theheapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.
– Patrick Haugh
Nov 23 '18 at 16:47
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
But why is it working in the first case (in my question)? The list isn't ordered.
– d4rty
Nov 23 '18 at 16:41
1
1
Because your example list happens to satisfy the heap invariant when
<
isn't reversed, but isn't a valid heap when <
is reversed. The Theory section of the heapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.– Patrick Haugh
Nov 23 '18 at 16:47
Because your example list happens to satisfy the heap invariant when
<
isn't reversed, but isn't a valid heap when <
is reversed. The Theory section of the heapq
documentation would be a good place to start reading, though I also recommend you actually try implementing all of the functions to understand how they work.– Patrick Haugh
Nov 23 '18 at 16:47
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53450056%2fhow-to-invert-the-order-of-elements-in-a-heapq-heap-with-object-comparison-funct%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