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, 'p…