跳转至

🔥AI副业赚钱星球

点击下面图片查看

郭震AI

发现列表前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)

大家在看

京ICP备20031037号-1 | AI之家 | AI资讯 | Python200 | 数据分析