在python中队列是如何工作的?
我正在尝试学习Python的东西,想知道是否有人能帮我举一些使用优先级队列的例子。我知道它们在java中是如何工作的,但似乎看不到它们在Python中工作的清晰示例。就像我在这里看到的获取队列大小的是.qsize():但它没有显示获取最小值、最大值、组织、弹出、添加到队列中、排序、迭代的示例。如果有人能给我举一个这样的例子,或者给我指出正确的学习方向,我将不胜感激。队列模块中的队列主要面向多线程的生产者/消费者模式的实现。如果您对通用优先级队列数据结构感兴趣,.在python中队列是如何工作的?,python,Python,我正在尝试学习Python的东西,想知道是否有人能帮我举一些使用优先级队列的例子。我知道它们在java中是如何工作的,但似乎看不到它们在Python中工作的清晰示例。就像我在这里看到的获取队列大小的是.qsize():但它没有显示获取最小值、最大值、组织、弹出、添加到队列中、排序、迭代的示例。如果有人能给我举一个这样的例子,或者给我指出正确的学习方向,我将不胜感激。队列模块中的队列主要面向多线程的生产者/消费者模式的实现。如果您对通用优先级队列数据结构感兴趣,.队列不是您要查找的优先级队列。相反
队列
不是您要查找的优先级队列。相反,您要寻找的是在Pythonlist
s上工作的。您可以使用空列表([]
)或现有列表(heapify
),其中将显示:
堆是二叉树,每个父节点的值都小于或等于其任何子节点。此实现使用的数组
heap[k]可以使用PriorityQueue。这是一个例子
In [1]: from Queue import PriorityQueue
In [2]: pq = PriorityQueue()
In [3]: pq.put((1, "girl")) # put data in the form of tuple (priority, data)
In [4]: pq.put((3, "forest"))
In [5]: pq.put((2, "rain"))
In [6]: pq.get() # retrieves data. lowest priority number first.
Out[6]: (1, 'girl')
In [7]: pq.empty() # checks if empty
Out[7]: False
In [8]: pq.full() # checks if full
Out[8]: False
In [9]: pq.qsize() # current size of the queue.
Out[9]: 2
此外,您还可以扩展PriorityQueue类并自定义内容。它显示了这些命令所需的语法。这还不够吗?@waleedkhan我看不出它在哪里显示了添加、删除、迭代、获取混合、最大、项目总数等基本操作。堆支持最小、最大和其他优先队列操作。漂亮的解释,只允许做+1。也许您希望阐明优先级队列的用例。PriorityQueue
是一个类而不是一个模块。此外,正如其他海报所建议的那样,heapq
更适合OP的需要。@ravoori谢谢。那是个错误。希望你能读到我答案的最后一行。OP明确要求“获取最小值、最大值、组织、弹出、添加到队列中、对它们排序、遍历它们”,而queue.PriorityQueue
对象实际上不会导出。因此,他们是OP工作的错误工具。
1
/ \
3 2
/ \
9 10
>>> L[0]
1
>>> heapq.nlargest(1, L)
[10]
>>> heapq.heappop(L)
1
def heappop(heap):
"""Pop the smallest item off the heap, maintaining the heap invariant."""
lastelt = heap.pop() # raises appropriate IndexError if heap is empty
if heap:
returnitem = heap[0]
heap[0] = lastelt
_siftup(heap, 0)
else:
returnitem = lastelt
return returnitem
def _siftup(heap, pos):
endpos = len(heap)
startpos = pos
newitem = heap[pos]
# Bubble up the smaller child until hitting a leaf.
childpos = 2*pos + 1 # leftmost child position
while childpos < endpos:
# Set childpos to index of smaller child.
rightpos = childpos + 1
if rightpos < endpos and not cmp_lt(heap[childpos], heap[rightpos]):
childpos = rightpos
# Move the smaller child up.
heap[pos] = heap[childpos]
pos = childpos
childpos = 2*pos + 1
# The leaf at pos is empty now. Put newitem there, and bubble it up
# to its final resting place (by sifting its parents down).
heap[pos] = newitem
_siftdown(heap, startpos, pos)
>>> heapq.heappush(L, 7)
>>> L
[3, 7, 10, 9]
def _siftdown(heap, startpos, pos):
newitem = heap[pos]
# Follow the path to the root, moving parents down until finding a place
# newitem fits.
while pos > startpos:
parentpos = (pos - 1) >> 1
parent = heap[parentpos]
if cmp_lt(newitem, parent):
heap[pos] = parent
pos = parentpos
continue
break
heap[pos] = newitem
[heappop(L) for i in range(len(L))]
for x in L:
print x
In [1]: from Queue import PriorityQueue
In [2]: pq = PriorityQueue()
In [3]: pq.put((1, "girl")) # put data in the form of tuple (priority, data)
In [4]: pq.put((3, "forest"))
In [5]: pq.put((2, "rain"))
In [6]: pq.get() # retrieves data. lowest priority number first.
Out[6]: (1, 'girl')
In [7]: pq.empty() # checks if empty
Out[7]: False
In [8]: pq.full() # checks if full
Out[8]: False
In [9]: pq.qsize() # current size of the queue.
Out[9]: 2