用headp找到最大最小的N个值 import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] print(heapq.nlargest(3, nums)) print(heapq.nsmallest(3, nums)) [42, 37, 23] [-4, 1, 2] 数据结构复杂时 可以用key这个参数,传入一个lambda表达式 portfolio = [ {'name': 'IBM', 'shares': 100, 'price':…