问题:找出列表中最大或者最小的N个数
import heapq
li = [1,10,15,2,5,11]
print(heapq.nlargest(3,li)) #[15, 11, 10]
print(heapq.nsmallest(3,li)) #[1, 2, 5]
还可以接收key,从而允许他们工作在更加复杂的数据结构上
test = [
{"name":"book","price":10},
{"name":"water","price":5},
{"name":"sofa","price":10000},
{"name":"bed","price":100},
]
print(heapq.nlargest(2,test,key=lambda s:s["price"]))
print(heapq.nsmallest(2,test,key=lambda s:s["price"]))