【python】堆heap

记录序列的前5大

import heapq
list_o = [1,6,4,9,10,8,4]
list_s = []
for num in list_o:        
    heapq.heappush(list_s, num)
    if(len(list_s) > 5):
        heapq.heappop(list_s)
print(list_s)
发布了178 篇原创文章 · 获赞 30 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/ACBattle/article/details/103241178
今日推荐