heapify( )

heapify()函数用于将一个序列转化为初始化堆

nums=[16,7,3,20,17,8,-1]
print('nums:',nums)
show_tree(nums) nums: [16, 7, 3, 20, 17, 8, -1] 16
7 3
20 17 8 -1
------------------------------------ heapq.heapify(nums)
print('nums:',nums)
show_tree(nums) nums: [-1, 7, 3, 20, 17, 8, 16] -1
7 3
20 17 8 16
------------------------------------

heappush( )

heappush()是实现将元素插入到堆的操作
heappush()操作前一定要先将序列初始化成堆!heappush是对于"堆"的操作!不然是没有意义

nums=[16,7,3,20,17,8,-1]
print(nums)
show_tree(nums) [16, 7, 3, 20, 17, 8, -1] 16
7 3
20 17 8 -1
------------------------------------
heapq.heapify(nums)
print('初始化成堆:',nums)
show_tree(nums) 初始化成堆: [-1, 7, 3, 20, 17, 8, 16] -1
7 3
20 17 8 16
------------------------------------
for i in random.sample(range(1,8),2):
print("本次push:",i)
heapq.heappush(nums,i)
print(nums)
show_tree(nums) 本次push: 5
[-1, 5, 3, 7, 17, 8, 16, 20] -1
5 3
7 17 8 16
20
------------------------------------ 本次push: 7
[-1, 5, 3, 7, 17, 8, 16, 20, 7] -1
5 3
7 17 8 16
20 7
------------------------------------

heappop( )

heappop()是实现将元素删除出堆的操作
同样的操作前一定要先将序列初始化成堆,否则也没什么意义

nums=[16,7,3,20,17,8,-1]
print(nums)
show_tree(nums) [16, 7, 3, 20, 17, 8, -1] 16
7 3
20 17 8 -1
------------------------------------ heapq.heapify(nums)
print('初始化成堆:',nums)
show_tree(nums) 初始化成堆: [-1, 7, 3, 20, 17, 8, 16] -1
7 3
20 17 8 16
------------------------------------
for i in range(0,2):
print("本次pop:",heapq.heappop(nums))
print(nums)
show_tree(nums) 本次pop: -1
[3, 7, 8, 20, 17, 16] 3
7 8
20 17 16
------------------------------------ 本次pop: 3
[7, 16, 8, 20, 17] 7
16 8
20 17
------------------------------------

nlargest( )/nsmallest( )

sorted(iterable, key=key, reverse=True)[:n]

  • nlargest(n,iterable) 求序列iterable中的TopN | nsmallest(n,iterable) 求序列iterable中的BtmN
import heapq
nums=[16,7,3,20,17,8,-1]
print(heapq.nlargest(3,nums))
print(heapq.nsmallest(3,nums)) [20, 17, 16]
[-1, 3, 7]
  • nlargest(n, iterable, key=lambda) | nsmallest(n, iterable, key=lambda) key接受关键字参数,用于更复杂的数据结构中
def print_price(dirt):
for i in dirt:
for x,y in i.items():
if x=='price':
print(x,y) 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 x:x['price'])
expensive=heapq.nlargest(3,portfolio,key=lambda y:y['price'])
print_price(cheap)
print_price(expensive) price 16.35
price 21.09
price 31.75 price 543.22
price 115.65
price 91.1

关于heap和heap sort

对于上面的nums=[16,7,3,20,17,8,-1]序列,图解了:

构造堆的操作(点击查看)

push堆的操作(点击查看)

pop堆的操作(点击查看)

参考文章

详解Python中heapq模块的用法(包括show_tree())

详解堆排序

浅谈算法和数据结构: 五 优先级队列与堆排序

最新文章

  1. ASP.NET webform基于Jquery,AJAX的三级联动
  2. asp.net(C#)利用QRCode生成二维码(续)-在二维码图片中心加Logo或图像
  3. IOS第一天多线程-04GCD通信
  4. Linux下一些文件夹的含义
  5. android中在代码中设置margin属性
  6. python 提取图片转为16 24BPP 的方法
  7. MyBatis之四:调用存储过程含分页、输入输出参数
  8. eclipse 远程wifi调试android程序
  9. android107 指针入门
  10. Java整型与字符串相互转换(转)
  11. OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(三)——计算节点的安装
  12. Jssdk微信分享
  13. DataGrid( 数据表格) 组件[6]
  14. [LeetCode][Java] 3Sum Closest
  15. spring3.0注解定时任务配置及说明
  16. FAT32系统中长文件名的存储(转)
  17. Java List Remove时要注意的细节
  18. poj3249 拓扑排序+DP
  19. requests-post请求
  20. Effective C++ ——构造/析构/赋值运算符

热门文章

  1. ASP.NET Core入门(一)
  2. 云课堂Android模块化实战--如何设计一个通用性的模块
  3. WebStrom创建自定义后缀名的文件
  4. Windows10使用
  5. Django-02路由层
  6. CentOS 图形界面的关闭与开启
  7. centos6.3 配置 smb 服务
  8. fastcgi main
  9. MPMoviePlayerViewController不能播放本地mp4的解决办法.
  10. Eclipse中的常见设置