Fork自 CSDN 技术社区 / skill_tree_python
体验新版 GitCode,发现更多精彩内容 >>
# -*- coding: UTF-8 -*-
# 作者:huanhuilong
# 标题:numpy 应用之 topn # 描述:取 list 的 topn import numpy as np def get_top_n(array, top_n): top_n_indexs = np.argsort(array)[:-(top_n+1):-1] results = [array[index] for index in top_n_indexs] return results if __name__ == '__main__': ret = get_top_n(np.array([1, 3, 34, 4, 5, 6]), 3) print(ret)