Python Cookbook 数据结构和算法

1.查找最大或最小的N个元素

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).查找最小的三个可以写成

heapq.heappop(heap)
heapq.heappop(heap)
heapq.heappop(heap)

==>当查找的元素个数相对比较小的时候,nlargest()和nsmallest比较合适.

==>仅查找最大值或最小值, min()和max()函数会更快

==>如果查找的数量跟集合本身差不多大,应该先排序,再使用切片操作sorted(items)[:N]和sorted(items)[-N:]

猜你喜欢

转载自www.cnblogs.com/wuzdandz/p/10785154.html