gpt4 book ai didi

Python PriorityQueue 顺序

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:59 24 4
gpt4 key购买 nike

我试图弄清楚 PriorityQueue.get() 在 Python 中返回值的顺序。首先,我认为较小的优先级值会首先返回,但经过几个示例后,情况并非如此。这是我运行的示例:

>>> qe = PriorityQueue()
>>> qe.put("Br", 0)
>>> qe.put("Sh", 0.54743812441605)
>>> qe.put("Gl", 1.1008112004388)

>>> qe.get()
'Br'
>>> qe.get()
'Gl'
>>> qe.get()
'Sh'

为什么它按这个顺序返回值?

最佳答案

根据doc ,第一个参数是优先级,第二个参数是值。这就是为什么你会得到这样的结果。

A typical pattern for entries is a tuple in the form: (priority_number, data).

所以你应该传递一个元组来放置,如下所示:

>>> q = PriorityQueue()
>>> q.put((10,'ten'))
>>> q.put((1,'one'))
>>> q.put((5,'five'))
>>> q.get()
>>> (1, 'one')
>>> q.get()
>>> (5, 'five')
>>> q.get()
>>> (10, 'ten')

注意额外的大括号。

关于Python PriorityQueue 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38358523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com