- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 Python 3(.6) 中使用 queue.PriorityQueue
。
我想存储具有给定优先级的对象。但是,如果两个对象具有相同的优先级,我不介意 PriorityQueue.get
返回任何一个。换句话说,我的对象不能以整数进行比较,允许它们比较是没有意义的,我只关心优先级。
在Python 3.7's documentation ,有一个解决方案涉及 dataclasses
。我引用:
If the data elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number:
from dataclasses import dataclass, field
from typing import Any
@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any=field(compare=False)
唉,我正在使用 Python 3.6。在 the documentation of this version of Python ,没有关于使用 PriorityQueue
作为优先级的评论,也没有理会“对象值”,这在我的情况下是不合逻辑的。
有没有比在我的自定义类上定义 __le__
和其他比较方法更好的方法?我发现这个解决方案特别丑陋且违反直觉,但这可能就是我。
最佳答案
dataclasses
只是一种避免必须创建大量样板代码的便捷方法。
您实际上不必创建类。一个也有唯一计数器值的元组:
from itertools import count
unique = count()
q.put((priority, next(unique), item))
因此,相同优先级之间的关系被后面的整数打破;因为它始终是唯一的,所以永远不会咨询 item
值。
您还可以使用直接丰富的比较方法创建一个类,使用 @functools.total_ordering
变得更简单:
from functools import total_ordering
@total_ordering
class PrioritizedItem:
def __init__(self, priority, item):
self.priority = priority
self.item = item
def __eq__(self, other):
if not isinstance(other, __class__):
return NotImplemented
return self.priority == other.priority
def __lt__(self, other):
if not isinstance(other, __class__):
return NotImplemented
return self.priority < other.priority
关于python - 使用 queue.PriorityQueue,不关心比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54027861/
采用另一个优先级队列的 java API PriorityQueue 构造函数是否会破坏参数?如果是这样,它的clone()方法足以创建浅拷贝吗? 最佳答案 不,它不是破坏性的。几乎所有集合类都有复制
我正在尝试复制一个 PriorityQueue 对象。 我的目标是在不修改我原来的 PriorityQueue 的情况下更改我的 Copy 的某些对象 为了这样做,我复制了我的 PriorityQue
我正在解决leetcode的Merge K Sorted Lists problem . 使用 Python2 的 Queue 模块中的 PriorityQueue 的相同算法会为 Python3 的
当 PriorityQueue.size() > 0 在 Android 上时,当 PriorityQueue.peek() 返回 null 时,我遇到了问题。 我认为这可能是设备问题。有人有什么想法
我有一个asyncio.PriorityQueue,用作网络爬虫的URL队列,当我调用url_queue.get时,得分最低的URL首先从队列中删除()。当队列达到 maxsize 项时,默认行为是阻
完全二叉树 一棵深度为k的有n个结点的 二叉树 ,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与 满二叉树 中编号为i的结点在二叉树中的位置相同,则这棵
我很难理解 priorityqueue 如何使用 compareTo 方法对其内容进行排序。 我在上一门名为 Node.js 的类(class)。它有 4 个字段。 private char char
我目前有一种方法使用 scala.collection.mutable.PriorityQueue 按特定顺序组合元素。例如代码看起来有点像这样: def process[A : Ordering]
没有提供自定义比较器,优先级队列按升序插入元素,但是,在删除特定元素后,顺序会发生变化。 PriorityQueue pq = new PriorityQueue<>(); pq.add(10); p
这个问题已经有答案了: The built-in iterator for java's PriorityQueue does not traverse the data structure in a
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我指的是此博客上列出的代码:https://strstr.io/Leetcode1054-Distant-Barcodes/ 我在这里复制这段代码 class Solution { publi
这个问题已经有答案了: Why does PriorityQueue.toString return the wrong element order? [duplicate] (4 个回答) 已关闭
我有一个比较器类 NComparator,它比较 2 个 Node 对象并返回 1、-1 或 0。 我初始化了一个初始容量为 100 的 PriorityQueue 和那个 NComparator。
当项目是整数与字符串时,PriorityQueue 的不同行为让我非常困惑。但在解决这个问题之前,我想了解以下行为(使用项目作为整数)。 假设我有一个包含以下数据的优先级队列(对于每个元素,第一个值是
我有使用 PriorityQueue 的程序。 poll() 没有给出队列中的所有值。 class Coffee { public static void main(String[] args
所以我正在尝试构建我的第一个 prim 算法,为此我根据其权重按优先级对边缘进行排序。 所以我认为如果我使用优先级队列会很有帮助,为此我需要让我的边缘实现 Comparable<> 接口(interf
编译器(Java 8)提示以下代码没有合适的构造函数: PriorityQueue heap = new PriorityQueue((ListNode n1, ListNode n2) -> n1.
我有一个优先级队列,我在其中添加一个节点对象,其中节点应按它们包含的值排序。由于某种原因,优先级队列不会在添加时对节点进行排序。如果有人能发现其中的问题或有任何指导,我很感激。这是一个简短的示例: P
这是我在这里发表的第一篇文章,因此请随时为我指出关于在这里提出问题的正确方向。 我的问题出在 java.util.PriorityQueue 上。 我有一个初始化的队列; myComparab
我是一名优秀的程序员,十分优秀!