Python 数组排序

版权声明: https://blog.csdn.net/u011286584/article/details/82874080

定期整理点滴,完善自己,今后给洋哥挣钱,陪伴着让我的小宝贝发自内心爱上笑,加油吧

这里写图片描述

数组排序

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 于广州

猜你喜欢

转载自blog.csdn.net/u011286584/article/details/82874080