这个模块(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问题)

@link http://m.blog.csdn.net/blog/Calling_Wisdom/41676133

python heapq的更多相关文章

  1. [Python] heapq简介

    [Python] heapq简介 « Lonely Coder [Python] heapq简介 judezhan 发布于 2012 年 8 月 8 日 暂无评论 发表评论 假设你需要维护一个列表,这 ...

  2. Python heapq 模块的实现 - A Geek's Page

    Python heapq 模块的实现 - A Geek's Page Python heapq 模块的实现

  3. 从一个集合中查找最大最小的N个元素——Python heapq 堆数据结构

    Top N问题在搜索引擎.推荐系统领域应用很广, 如果用我们较为常见的语言,如C.C++.Java等,代码量至少也得五行,但是用Python的话,只用一个函数就能搞定,只需引入heapq(堆队列)这个 ...

  4. python heapq模块使用

    Python内置的heapq模块 Python3.4版本中heapq包含了几个有用的方法: heapq.heappush(heap,item):将item,推入heap >>> it ...

  5. Python heapq模块

    注意,默认的heap是一个小顶堆! heapq模块提供了如下几个函数: heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表) heapq.heappo ...

  6. heapy() :python自带的堆排序

    堆是一个二叉树,其中每个父节点的值都小于或等于其所有子节点的值.整个堆的最小元素总是位于二叉树的根节点.python的heapq模块提供了对堆的支持. 堆数据结构最重要的特征是heap[0]永远是最小 ...

  7. [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

    [译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...

  8. 352. Data Stream as Disjoint Intervals (TreeMap, lambda, heapq)

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  9. python内置模块笔记(持续更新)

    常用函数name = '{wh}my \t name is {name},age is {age}.' print(name.capitalize()) # 字符串的开头字母大写 print(name ...

随机推荐

  1. vim 多窗口编辑

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  2. memcached 内存管理 分析(转)

    Memcached是一个高效的分布式内存cache,了解memcached的内存管理机制,便于我们理解memcached,让我们可以针对我们数据特点进行调优,让其更好的为我所用.这里简单谈一下我对me ...

  3. UIButton 设置字体大小

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  4. JavaScript 【跨浏览器处理XML,做个兼容】

    //兼容方法        function getXMLDOM(xmlStr) {            var xmlDom = null;            if (typeof windo ...

  5. RequiredFieldValidator验证下拉列表框

    <asp:DropDownList ID="DropDownList1" runat="server" CssClass="style01&qu ...

  6. SWFUpload的使用及其注意事项

    SWFUpload的使用: 添加Jquery    swfuploaad.js  handler.js文件 配置参数: upload_url:文件将要被传到的处理程序 post_params:{“”: ...

  7. 记一次phpStudy apache启动后自动关闭 修改过程

    第一种可能原因:路径包含中文 .添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文 ...

  8. HDU 1829 - A Bug's Life

    Problem Description Background Professor Hopper is researching the sexual behavior of a rare species ...

  9. BF算法(朴素的模式匹配算法)

    #include <stdio.h> #include <stdlib.h> int Index_BF(char S[],char T[])//s为目标串(长串),t为模式串( ...

  10. 阿牛的EOF牛肉串

    #include <iostream>using namespace std;long long s0,s1,s2,s3;int main(){ int i,n; while(cin> ...