Skip to content

Commit feb6183

Browse files
committed
python builtin PriorityQueue
1 parent d42337b commit feb6183

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

docs/16_优先级队列/priority_queue.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,28 @@ def test_priority_queue():
129129
while not pq.is_empty():
130130
res.append(pq.pop())
131131
assert res == ['purple', 'orange', 'black', 'white']
132+
133+
134+
def test_buildin_PriorityQueue(): # python3
135+
# https://pythonguides.com/priority-queue-in-python/
136+
from queue import PriorityQueue
137+
q = PriorityQueue()
138+
q.put((10, 'Red balls'))
139+
q.put((8, 'Pink balls'))
140+
q.put((5, 'White balls'))
141+
q.put((4, 'Green balls'))
142+
while not q.empty():
143+
item = q.get()
144+
print(item)
145+
146+
147+
def test_buildin_heapq_as_PriorityQueue():
148+
import heapq
149+
s_roll = []
150+
heapq.heappush(s_roll, (4, "Tom"))
151+
heapq.heappush(s_roll, (1, "Aruhi"))
152+
heapq.heappush(s_roll, (3, "Dyson"))
153+
heapq.heappush(s_roll, (2, "Bob"))
154+
while s_roll:
155+
deque_r = heapq.heappop(s_roll)
156+
print(deque_r)
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# Python 常用内置算法和数据结构
2+
23
相信到这里大家对常用的数据结构和算法及其实现都比较熟悉了。
34
之前在每章的数据结构和算法中涉及到的章节我都会提到对应的 python 内置模块,一般如果内置的可以满足需求,我们优先使用内置模块,
45
因为在性能和容错性方面内置模块要好于我们自己实现(比如有些是 c 实现的)。本章我们不会再对每个模块的原理详细说明,仅列举出一些常见模块供大家参考,
56
如果有需要最好的学习方式就是参考 Python 的官方文档。很多高级的数据结构我们也可以通过 google 搜索现成的库拿来直接用。
67

78
- 常用内置数据类型:list, tuple, dict, set, frozenset
8-
- collections
9-
- heapq
10-
- bisect
9+
- collections 模块:Counter(计数器), deque(双端队列), OrderedDict(有序字典),defaultdict(默认值字典)
10+
- heapq: 堆操作
11+
- bisect: 二分查找
1112

12-
下边我列了一个常用的表格,如果有遗漏可以在 issue 中提出。确保你了解这些数据结构和算法的使用以及时间、空间复杂度。
13+
下边我列了一个常用 python 内置数据结构和算法的表格,如果有遗漏可以在 issue 中提出。确保你了解这些数据结构和算法的使用以及时间、空间复杂度。
1314

14-
| 数据结构/算法 | 语言内置 | 内置库 |
15-
|----------------|---------------------------------|---------------------------------------------------------------|
16-
| 线性结构 | list(列表)/tuple(元祖) | array(数组,不常用)/collections.namedtuple |
17-
| 链式结构 | | collections.deque(双端队列) |
18-
| 字典结构 | dict(字典) | collections.Counter(计数器)/OrderedDict(有序字典)/defaultdict |
19-
| 集合结构 | set(集合)/frozenset(不可变集合) | |
20-
| 排序算法 | sorted | |
21-
| 二分算法 | | bisect模块 |
22-
| 堆算法 | | heapq模块 |
23-
| 缓存算法 | | functools.lru_cache(Least Recent Used, python3) |
15+
| 数据结构/算法 | 语言内置 | 内置库 |
16+
|---------------|---------------------------------|---------------------------------------------------------------|
17+
| 线性结构 | list(列表)/tuple(元祖) | array(数组,不常用)/collections.namedtuple |
18+
| 链式结构 | | collections.deque(双端队列) |
19+
| 字典结构 | dict(字典) | collections.Counter(计数器)/OrderedDict(有序字典)/defaultdict |
20+
| 集合结构 | set(集合)/frozenset(不可变集合) | |
21+
| 排序算法 | sorted | |
22+
| 二分算法 | | bisect模块 |
23+
| 堆算法 | | heapq模块 |
24+
| 优先级队列 | | queue.PriorityQueue |
25+
| 缓存算法 | | functools.lru_cache(Least Recent Used, python3) |

0 commit comments

Comments
 (0)