使用heapq获取最大N项和最小N项
>>> import heapq>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]>>> print heapq.nlargest(3, nums)[42, 37, 23]>>> print heapq.nsmallest(3, nums)[-4, 1, 2]
本文共 224 字,大约阅读时间需要 1 分钟。
使用heapq获取最大N项和最小N项
>>> import heapq>>> nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]>>> print heapq.nlargest(3, nums)[42, 37, 23]>>> print heapq.nsmallest(3, nums)[-4, 1, 2]
转载于:https://www.cnblogs.com/4thing/p/5641341.html