简体   繁体   English

python3的优先级队列大小按优先级?

[英]python3 priorityqueue size by priority?

is there a way to take the .size() of the priority queue ... for each priority level? 有没有办法为每个优先级采取优先级队列的.size()?

say i have an object, which i will put in a priority queue, with a priority based on the state of some_variable ... 说我有一个对象,我将把它放在优先级队列中,该优先级基于some_variable的状态...

q = PriorityQueue()

if some_variable:
    q.put((1, my_object))
else:
    q.put((2, my_object))

can i then find out, by asking for some kind of .qsize() method, such as a variant maybe of... 然后我可以通过请求某种.qsize()方法来找出答案,例如...的变体吗?

q.qsize() 

but instead of the total size of the queue, i'd like to know the size for each of the two separate 'groups'/priorities (qsize where priority==1, qsize where priority==2), in this case. 但是在这种情况下,我想知道两个单独的“组” /优先级中每个组的大小(而不是队列的总大小)(qsize,其中priority == 1,qsize,其中priority == 2)。

as opposed to just the total q.qsize()? 而不只是总q.qsize()? hopefully that makes sense. 希望这是有道理的。

The simplest way I can see to do this would be 我可以看到的最简单的方法是

from collections import Counter
sizeByPriority = Counter(priority for priority, _elem in q.queue)

but of course this requires iterating over the entire queue, which may be prohibitive in your situation, and probably isn't thread-safe. 但这当然需要遍历整个队列,这在您的情况下可能是禁止的,并且可能不是线程安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM