How do I make a heap queue of objects in python?

John Hoffman

I want to use a heap queue (http://docs.python.org/2/library/heapq.html) on a class I made in python:

class Dog:
  def __init__(self, name, age):
    self.name = name
    self.age = age

I want to compare the dogs by age in the heap queue. How do I tell python to compare my objects by age? In other words, can I somehow write a "comparator" in python?

Blender

You will have to add a __lt__ method to your class:

def __lt__(self, other):
    return self.age < other.age

If you want to be safe, add a __eq__ method as well and decorate your class with functools.total_ordering to add the other comparison operators as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How Do I Queue My Python Locks?

From Dev

How do I hide the heap methods from the user when writing a priority queue?

From Dev

How do I improve my Python code about heap sort?

From Dev

How do I differentiate objects in python on canvas

From Dev

How do I retain global access to my pointers to objects created on the Heap

From Dev

How do you use a binary heap to implement a priority queue?

From Dev

How do you use a binary heap to implement a priority queue?

From Dev

How do I make a list of list objects In VB.NET?

From Dev

How do I make objects disappear after another one touches it?

From Dev

How do I make a function to show/hide different objects?

From Dev

How do I make players on Roblox spawn as different objects?

From Dev

How do I make a list of objects parcelable in Android?

From Dev

How do I make a schema with objects in array? Mongoose

From Dev

How Do I Make an Array of Objects a Reactive Data Source?

From Dev

How do I make my MergeSort generic for different objects?

From Dev

AS3 How do I make these objects loop endlessly?

From Dev

How do i print a queue?

From Dev

How do I make a python script executable?

From Dev

Numerical Python - how do I make this a ufunc?

From Dev

How do I make an object mutable in python?

From Dev

how do i make a nested dictionary in python?

From Dev

How do I make a float in Python?

From Dev

How do i make a game director in python?

From Dev

How do I make a grid in python?

From Dev

How do I make this python app installable?

From Dev

How do I make a timer in python?

From Dev

Why can't I make a priority queue of type struct objects?

From Dev

Why can't I make a priority queue of type struct objects?

From Dev

Python: how to use pickle to dump Queue objects?

Related Related

  1. 1

    How Do I Queue My Python Locks?

  2. 2

    How do I hide the heap methods from the user when writing a priority queue?

  3. 3

    How do I improve my Python code about heap sort?

  4. 4

    How do I differentiate objects in python on canvas

  5. 5

    How do I retain global access to my pointers to objects created on the Heap

  6. 6

    How do you use a binary heap to implement a priority queue?

  7. 7

    How do you use a binary heap to implement a priority queue?

  8. 8

    How do I make a list of list objects In VB.NET?

  9. 9

    How do I make objects disappear after another one touches it?

  10. 10

    How do I make a function to show/hide different objects?

  11. 11

    How do I make players on Roblox spawn as different objects?

  12. 12

    How do I make a list of objects parcelable in Android?

  13. 13

    How do I make a schema with objects in array? Mongoose

  14. 14

    How Do I Make an Array of Objects a Reactive Data Source?

  15. 15

    How do I make my MergeSort generic for different objects?

  16. 16

    AS3 How do I make these objects loop endlessly?

  17. 17

    How do i print a queue?

  18. 18

    How do I make a python script executable?

  19. 19

    Numerical Python - how do I make this a ufunc?

  20. 20

    How do I make an object mutable in python?

  21. 21

    how do i make a nested dictionary in python?

  22. 22

    How do I make a float in Python?

  23. 23

    How do i make a game director in python?

  24. 24

    How do I make a grid in python?

  25. 25

    How do I make this python app installable?

  26. 26

    How do I make a timer in python?

  27. 27

    Why can't I make a priority queue of type struct objects?

  28. 28

    Why can't I make a priority queue of type struct objects?

  29. 29

    Python: how to use pickle to dump Queue objects?

HotTag

Archive