编程语言
首页 > 编程语言> > Python杂技1

Python杂技1

作者:互联网

In [4]: import heapq
In [6]: topK = 2
In [7]: item_score_dict = {'a':1,'b':2} # 从字典中
In [8]: rank = heapq.nlargest(topK,item_score_dict,key = item_score_dict.get) # 返回的是[b,a]

>>> import heapq # 从列表中
>>> nums=[1,8,2,23,7,-4,18,23,42,37,2]
>>> print(heapq.nlargest(3,nums))[42, 37, 23]
>>> print(heapq.nsmallest(3,nums))[-4, 1, 2]
# 一种比较好的打印方式
sys.stdout.write('\r{} / {} : loss = {}'.format(
                    i, num_batches, np.mean(losses[-verbose:])
                ))
sys.stdout.flush()

 

标签:heapq,杂技,nums,Python,23,item,score,dict
来源: https://blog.csdn.net/weixin_40539952/article/details/115013559