python学习——heapq模块
2015-09-12 16:26
666 查看
heapq是python中包含堆的相关操作的模块,其典型的应用就是取top K个数:
对于其它的数据类型可以如下操作:
heapify(…)
\qquadTransform list into a heap, in-place, in O(len(heap)) time.
heappop(…)
\qquadPop the smallest item off the heap, maintaining the heap invariant.
heappush(…)
\qquadPush item onto heap, maintaining the heap invariant.
heapreplace(…)
\qquadPop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement:
\qquadif item > heap[0]:
\qquad\qquaditem = heapreplace(heap, item)
nlargest(…)
\qquadFind the n largest elements in a dataset. Equivalent to: sorted(iterable, reverse=True)[:n]
nsmallest(…)
\qquadFind the n smallest elements in a dataset.
import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, nums)) # Prints [42, 37, 23] print(heapq.nsmallest(3, nums)) # Prints [-4, 1, 2]
对于其它的数据类型可以如下操作:
portfolio = [ {'name':'IBM', 'shares': 100, 'price': 91.1}, {'name':'AAPL', 'shares': 50, 'price': 543.22}, {'name':'FB', 'shares': 200, 'price': 21.09}, {'name':'HPQ', 'shares': 35, 'price': 31.75}, {'name':'YHOO', 'shares': 45, 'price': 16.35}, {'name':'ACME', 'shares': 75, 'price': 115.65} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
heapify(…)
\qquadTransform list into a heap, in-place, in O(len(heap)) time.
heappop(…)
\qquadPop the smallest item off the heap, maintaining the heap invariant.
heappush(…)
\qquadPush item onto heap, maintaining the heap invariant.
heapreplace(…)
\qquadPop and return the current smallest value, and add the new item. This is more efficient than heappop() followed by heappush(), and can be more appropriate when using a fixed-size heap. Note that the value returned may be larger than item! That constrains reasonable uses of this routine unless written as part of a conditional replacement:
\qquadif item > heap[0]:
\qquad\qquaditem = heapreplace(heap, item)
nlargest(…)
\qquadFind the n largest elements in a dataset. Equivalent to: sorted(iterable, reverse=True)[:n]
nsmallest(…)
\qquadFind the n smallest elements in a dataset.
>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] >>> import heapq >>> heap = list(nums) >>> heapq.heapify(heap) >>> heap [-4, 2, 1, 23, 7, 2, 18, 23, 42, 37, 8] >>> heapq.heappop(heap) -4 >>> heapq.heappop(heap) 1 >>> heapq.heappop(heap) 2
相关文章推荐
- python 内置函数filter和sorted的使用
- python学习——yield
- python-sub
- python 内置函数map、reduce的使用
- Python笔记---windows下安装setuptools和pip
- python 排列组合
- #!/usr/bin/env python
- python学习——基本结构
- python unittest源码解析四----关于源码中的__unittest的用处
- python-正则表达式中的分组 group
- Python爬虫入门之Beautiful Soup的用法
- python 中文乱码问题深入分析
- python第二章
- windows下安装python和Django
- leetcode hIndex implemented with python
- mac os x下python安装MySQLdb模块
- Python 爬虫 多线程爬取百度贴吧内容,并将内容存到本地
- Python 爬虫 raise self._value IndexError: list index out of range
- python 的字符串驻留机制
- python-04<关于字典:键必须是可哈希的>