当前位置: 首页 > news >正文

广告推广平台网站asp网站搭建工具

广告推广平台网站,asp网站搭建工具,上海电子商城网站制作,网站设计的分辨率heapqpython内置heapq模块#xff0c;通过import heapq导入。heapq模块是用于堆实现优先队列。我们知道队列是先进先出(FIFO)#xff0c;heapq中的优先队列指的是不论谁先进#xff0c;最小的先出或者最大的先出。# 需要注意的是heapq的堆是小根堆。01 23 4 5 67 8 9 10 11 …heapqpython内置heapq模块通过import heapq导入。heapq模块是用于堆实现优先队列。我们知道队列是先进先出(FIFO)heapq中的优先队列指的是不论谁先进最小的先出或者最大的先出。# 需要注意的是heapq的堆是小根堆。01 23 4 5 67 8 9 10 11 12 13 1415 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30# 堆顶位置的元素最小heapq和python一致下标索引从0开始。# heapq提供的主要API, Usage:heap [] # creates an empty heapheappush(heap, item) # pushes a new item on the heapitem heappop(heap) # pops the smallest item from the heapitem heap[0] # smallest item on the heap without popping itheapify(x) # transforms list into a heap, in-place, in linear timeitem heapreplace(heap, item) # pops and returns smallest item, and adds# new item; the heap size is unchangedheapq实现列表排序基本排序流程# -*- coding: utf-8 -*-# created by X. Liu on 2020/3/14import heapqimport randomli [i for i in range(10)]random.shuffle(li)print(排序前, li)# step1: 建堆(小根堆)heapq.heapify(li)print(小根堆, li)# step2: 排序# heapq.heappop()方法弹出堆顶元素即最小的元素new_li []for i in range(10):new_li.append(heapq.heappop(li))print(排序后, new_li)# output:排序前 [3, 4, 9, 0, 5, 1, 6, 2, 8, 7]小根堆 [0, 2, 1, 3, 5, 9, 6, 4, 8, 7]排序后 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]堆排序实现版本2 (不考虑时间复杂度)# 借助 heapq.heappush() heapq.heappop()# heappush(h, item) # Push the value item onto the heap, maintaining the heap invariant.import heapqdef heap_sort(li):h []# 建小根堆for i in li:heapq.heappush(h, i)# 依次弹出最小元素构成有序列表切片回lili[:] [heapq.heappop() for i in range(len(li))]对比分析heapq的heapify建堆函数和我们自己实现的建堆函数大同小异。我们自己的sift函数一步到位(实现堆顶元素选位的过程)heapify中使用两步判断比较其实不如我们自己写的sift好。heappop函数实现每次弹出堆顶元素弹出后再将整个堆调整为一个新的小根堆。它的目的是为了实现优先队列所以才会这样设计。我们如果希望借助它实现列表排序只能手动排序。补充heap的值可以是元组 h [] heappush(h, (5, write code)) heappush(h, (7, release product)) heappush(h, (1, write spec)) heappush(h, (3, create tests)) heappop(h)(1, write spec)heapq.heapify(x)底层代码实现def heapify(x):n len(x)for i in reversed(range(n//2)):_siftup(x, i)def _siftup(heap, pos):endpos len(heap)startpos posnewitem heap[pos]# Bubble up the smaller child until hitting a leaf.childpos 2*pos 1 # leftmost child positionwhile childpos endpos:# Set childpos to index of smaller child.rightpos childpos 1if rightpos endpos and not heap[childpos] heap[rightpos]:childpos rightpos# Move the smaller child up.heap[pos] heap[childpos]pos childposchildpos 2*pos 1# The leaf at pos is empty now. Put newitem there, and bubble it up# to its final resting place (by sifting its parents down).heap[pos] newitem_siftdown(heap, startpos, pos)def _siftdown(heap, startpos, pos):newitem heap[pos]# Follow the path to the root, moving parents down until finding a place# newitem fits.while pos startpos:parentpos (pos - 1) 1parent heap[parentpos]if newitem parent:heap[pos] parentpos parentposcontinuebreakheap[pos] newitemheapq.heappop底层代码实现def heappop(heap):Pop the smallest item off the heap, maintaining the heap invariant.lastelt heap.pop() # raises appropriate IndexError if heap is emptyif heap:returnitem heap[0]heap[0] lastelt_siftup(heap, 0)return returnitemreturn lastelt
http://www.zqtcl.cn/news/868010/

相关文章:

  • 门户网站的好处企业邮箱系统
  • 重庆企业建站公司有那些网站做结伴旅游的
  • 创意营销策划案例网站网页制作及优化
  • 网站上动画视频怎么做的建设兵团12师教育局网站
  • 博客网站开发思维导图app网站制作公司
  • 池州网站建设有哪些公司兴义网站seo
  • seo优化网站模板网站建设的七大优缺点
  • 天猫国际采取的跨境电商网络营销方式关键词排名优化公司推荐
  • 亳州建设网站做网站文字怎么围绕图片
  • 网站开发 项目计划外链建设给网站起的作用
  • 你好南京网站网站开发实施步骤和说明
  • 文化共享工程网站建设情况wordpress菠菜插件
  • 网站大气是什么意思哈尔滨做网站电话
  • 公司网站站群是什么化妆品网站设计欣赏
  • 网站公司未来计划ppt怎么做平潭做网站
  • 做网站和推广工资多少招聘网站建设价格
  • 网站建设 响应式 北京网架公司十大排名榜
  • 网站推广目标关键词是什么意思网站推广软件工具
  • 哪里可以做免费的物流网站wordpress为什么放弃
  • 做网站需要多少钱 都包括什么高端大气的网站首页
  • 黄石做网站联系最近的国际新闻
  • 网站建设与运营的预算方案淘宝禁止了网站建设类
  • 做网站的顺序编写app的软件
  • 站长联盟个人网站不备案
  • 惠州建设工程交易网站网站服务器失去响应
  • 网站下拉广告iphone app wordpress
  • 网站图片怎样做seo优化如何重新安装wordpress
  • python做网站源码长沙建设网站制作
  • wordpress调用分类的所有子目录龙岩seo公司首荐3火星
  • 聊城市建设工程质量监督站网站wordpress 头部