在Python中重载优先级队列的比较器

Few*_*Tem 2 python queue

C中的人将节点推入优先级队列,我们​​不得不重载<运算符.是否有类似于python优先级队列中的任何内容.

例如在C:

    struct node
    {

    int city , weight

    }

    bool operator < (node a, node b)
    {
     return a.weight > b.weight;
    }

    int main()
   {
     node a,b,c;
     priority_queue <node> pq;
     pq.push(a);pq.push(b);pq.push(c);
     return 0;
   }
Run Code Online (Sandbox Code Playgroud)

有没有类似的方法来定义Python中的优先级队列; 如果需要帮助,我无法在python.org文档的头部或尾部进行优先级排队.我在stackoverflow上看到了几个解释,需要更多的解释.谢谢.

Raf*_*ler 8

将数据包装在类中并覆盖__cmp__以返回您想要进行比较的内容.例如

class PQEntry:

    def __init__(self, priority, value):
        self.priority = priority
        self.value = value

    def __cmp__(self, other):
         return cmp(self.priority, other.priority)
Run Code Online (Sandbox Code Playgroud)