码迷,mamicode.com
首页 > 其他好文 > 详细

怎样从一个集合中获得最大或者最小的 N 个元素列表?

时间:2019-06-14 22:14:44      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:bsp   app   获得   复杂度   har   数据结构   时间   size   问题   

heapq 模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题。

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‘])

 

堆数据结构最重要的特征是 heap[0] 永远是最小的元素。并且剩余的元素可以很 容易的通过调用 heapq.heappop() 方法得到,该方法会先将第一个元素弹出来,然后 用下一个最小的元素来取代被弹出元素(这种操作时间复杂度仅仅是 O(log N),N 是 堆大小)。比如,如果想要查找最小的 3 个元素,你可以这样做:

 

怎样从一个集合中获得最大或者最小的 N 个元素列表?

标签:bsp   app   获得   复杂度   har   数据结构   时间   size   问题   

原文地址:https://www.cnblogs.com/wangdongpython/p/11025309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!