import heapq a_list = [3, 4, 2, 5, 1, 6] c_dict = {'A': 3, 'B': 4, 'C': 5} topNum = 2 print(heapq.nlargest(topNum, a_list)) print(heapq.nlargest(topNum, c_dict)) print(heapq.nlargest(topNum, c_dict.keys())) print(heapq.nlargest(topNum, c_dict.values())) print('=' * 15) print(heapq.nsmallest(topNum, a_list)) print(heapq.nsmallest(topNum, c_dict)) print(heapq.nsmallest(topNum, c_dict.keys())) print(heapq.nsmallest(topNum, c_dict.values()))
结果
[6, 5] ['C', 'B'] ['C', 'B'] [5, 4] =============== [1, 2] ['A', 'B'] ['A', 'B'] [3, 4]
标签:heapq,nsmallest,最小,列表,dict,topNum,print,nlargest,字典 From: https://www.cnblogs.com/daizichuan/p/17488727.html