pq.empty(): print(pq.get()[1]) Inside PriorityQueue: [(-5, 'Python'), (-4, 'C'), (-3, 'Js')]PythonCJs...(10, heap))print("N smallest: ", nsmallest(10, heap))print(len(heap)) # 不原地修改 N largest: [999, 999, 998..., 994, 992, 991, 990, 988, 985, 982]N smallest: [1, 1, 1, 2, 4, 5, 5, 6, 6, 9]1000 合并(排序)
merge 方法用于将两个..., 29, 66, 66, 70, 99]
最后两个方法 heapreplace 和 heappushpop 分别相当于:
lstA = [1,2,3,4,5]lstB = [1,2,3,4,5...****************lstA: [2, 4, 3, 9, 5] poped: 0lstB: [2, 4, 3, 5, 9] poped: 0
这两个方法的执行效率要比分开写的方法高,但要注意