python top k问题
【摘要】 numpy的:
import numpy as nptop_k=3arr = np.array([1, 3, 2, 4, 5])top_k_idx=arr.argsort()[::-1][0:top_k]print(top_k_idx)
#coding=utf-8'''Created on Sep 21, 2015@author: haoyifen'''i...
numpy的:
-
import numpy as np
-
top_k=3
-
arr = np.array([1, 3, 2, 4, 5])
-
top_k_idx=arr.argsort()[::-1][0:top_k]
-
print(top_k_idx)
-
#coding=utf-8
-
'''
-
Created on Sep 21, 2015
-
@author: haoyifen
-
'''
-
import time
-
import random
-
def left(i):
-
return 2*i+1
-
def right(i):
-
return 2*i+2
-
def min_heapify(a,heap_size,i):
-
l=left(i)
-
r=right(i)
-
if l<heap_size and a[l]<a[i] :
-
least=l
-
else:
-
least=i
-
if r<heap_size and a[r]<a[least]:
-
least=r
-
if least!=i:
-
a[i],a[least]=a[least],a[i]
-
min_heapify(a, heap_size, least)
-
def build_min_heap(a,heap_size):
-
for i in range((heap_size-1)//2,-1,-1):
-
min_heapify(a, heap_size, i)
-
def heap_sort(a,heap_size):
-
build_min_heap(a, heap_size)
-
for i in range(heap_size-1,0,-1):
-
a[i],a[0]=a[0],a[i]
-
#print a
-
heap_size-=1
-
min_heapify(a, hea
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/88933119
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)