定期整理点滴,完善自己,今后给洋哥挣钱,陪伴着让我的小宝贝发自内心爱上笑,加油吧
数组排序
import heapq
nums = [10, 2, 9, 100, 80]
print(heapq.nlargest(3, nums))
print(heapq.nsmallest(3, nums))
students = [
{'names': 'CC', "score": 100, 'height': 189},
{'names': 'BB', "score": 10, 'height': 169},
{'names': 'AA', "score": 80, 'height': 179}
]
print(heapq.nsmallest(2, students, key=lambda x: x['height']))
这个库非常好用,尤其取列表头部数据,最大几个,最小几个经常用到
运行结果
[100, 80, 10]
[2, 9, 10]
[{‘names’: ‘BB’, ‘score’: 10, ‘height’: 169}, {‘names’: ‘AA’, ‘score’: 80, ‘height’: 179}]
2018.09.27 于广州
版权声明:本文为u011286584原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。