Skip to content

Latest commit

 

History

History
34 lines (24 loc) · 677 Bytes

195.md

File metadata and controls

34 lines (24 loc) · 677 Bytes
@author jackzhenguo
@desc
@tag
@version 
@date 2020/04/02

195 发现列表前3个最大或最小数

使用堆模块 heapq 里的 nlargest 方法:

import heapq as hq
nums_list = [25, 35, 22, 85, 14, 65, 75, 22, 58]

# Find three largest values
largest_nums = hq.nlargest(3, nums_list)
print(largest_nums)

相应的求最小3个数,使用堆模块 heapq 里的 nsmallest 方法:

import heapq as hq
nums_list = [25, 35, 22, 85, 14, 65, 75, 22, 58]
smallest_nums = hq.nsmallest(3, nums_list)
print("\nThree smallest numbers are:", smallest_nums)
[上一个例子](194.md) [下一个例子](196.md)