python heapq
2016-06-18 18:50
435 查看
python heapq
这个模块(build-in)实现了一个堆的数据结构,完美的解决了Top-K问题,以后解决Top-K问题的时候,直接把这个模块拿来用就可以了 注意,默认的heap是一个小顶堆! heapq模块提供了如下几个函数: heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表) heapq.heappop(heap) 把堆顶元素弹出,返回的就是堆顶 heapq.heappushpop(heap, item) 先把item加入到堆中,然后再pop,比heappush()再heappop()要快得多 heapq.heapreplace(heap, item) 先pop,然后再把item加入到堆中,比heappop()再heappush()要快得多 heapq.heapify(x) 将列表x进行堆调整,默认的是小顶堆 heapq.merge(*iterables) 将多个列表合并,并进行堆调整,返回的是合并后的列表的迭代器 heapq.nlargest(n, iterable, key=None) 返回最大的n个元素(Top-K问题) heapq.nsmallest(n, iterable, key=None) 返回最小的n个元素(Top-K问题)
相关文章推荐
- 用 Python实现一个ftp+CRT(不用ftplib)
- Json概述以及python对json的相关操作
- python matplotlib 方程的绘制
- 爱的方程式(Python)
- 快速入门:十分钟学会Python
- sorting a python list by two criteria
- Python实现决策树
- python 爬虫学习之路
- 67. Add Binary [easy] (Python)
- 【contains-duplicate】leetCode python实现
- Python安装新的package
- Python学习笔记(While循环)
- 14. Longest Common Prefix [easy] (Python)
- threading in Python - 简单小例子
- 【leetCode】 Balanced Binary Tree python版实现
- python3 标准库 urllib.request
- 关于Python中函数默认参数的研究
- python笔记一_简单的基础
- python之流程控制语句
- 203. Remove Linked List Elements [easy] (Python)