def Topk(List, k, reverse = False):
"""
return the top k item in List and their indexes. If reverse, return the least k items
"""
List = list(List)
if len(List) < k:
raise valueError("The lenth of list is smaller than {}".format(k))
topk_list = []
index_list = []
# if we want the least k items, replace by the largest value. if we want the biggest k items, replace by the smallest values.
if reverse: replace_value = max(List)
else: replace_value = min(List)
for _ in range(k):
# if we want the least k items, we want the smallest value. else if we want the biggest k items, we want the biggest value.
if reverse: find_value = min(List)
else: find_value = max(List)
topk_list.append(round(find_value, 4))
# find the index of the first item that equal to find_value
find_index = Lis
python topk实现
最新推荐文章于 2023-12-12 21:51:43 发布