Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the value and index of Topk in list by python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "python how to find Topk values and indexes in list", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "python how to find Topk values and indexes in list" article.

Demand:

For a python list or numpy array, I need to find the maximum number of K in the list and its corresponding subscript.

Solution:

1. Dictionaries can be constructed and solved by sorting, but there is a large amount of code.

two。 Using the heapq library, you can directly get the subscript and numeric value of the maximum value.

Import heapqa = [4, range 2, 6] heapq.nlargest 9] # get the subscript, output as [4, 5, 2] heapq.nlargest (3, range (len (a)), a.

If you want to take the minimum number, you can use nsmallest

The idea of Python using Intermediate value to find TopK algorithm

First of all, we have to think, what am I going to do? Solve what problem?

TopK question, find out the first K maximum or minimum in a set of data, is this data repeated? Do you want to do a reprocessing?

Ok, we are clear about what we have done, so what is the process of the topK algorithm that python handles?

If you use sorting, then there is no need to introduce topK, when the data is powerful, selecting TopK can omit a lot of sorting calculations, as for how to optimize yourself to think, such as the difference between permutation and combination, one is extraction, the other is extraction and arrangement.

The following is to find out the maximum value of TopK as an example, and the minimum value can be modified by yourself.

The idea of the algorithm is to divide the sequence into three parts by using the intermediate value.

[list larger than intermediate value], intermediate value, [list smaller than intermediate value]

Then we should compare

The number of [lists larger than the median] = = k

You can get the first K maximum value, so

The point is to find out how to find the intermediate value.

Split into three parts, starting with the first number of the list as the intermediate value

The number of if [lists larger than the intermediate value] = = k:return intermediate value # program exit, end.

The number of if [lists larger than the median]

< k : ·····继续在【比中间值小的列表】找 ·····K - 【比中间值大的列表】的个数 -1 个数 (为什么要减一,1是前一次的中间值,分的三部分,前部分后部分都没有包含中间值,因此…) if 【比中间值大的列表】的个数 >

K:

... That is to say, a list larger than the median value is bigger than K, so just keep looking in this list.

Combine code and comments to see

If you want to find the minimum value, you only need to change it to ok, and you can also set a Boolean input to do the first K maximum and minimum value.

# 11 04#author half jin sweet potato roast # TopK algorithm, find out the first K maximum value in the sequence # input a seq# output divided into three parts with seq [0] as the intermediate value, the intermediate value, the seq that is larger than this value, the seq,# that is smaller than this value is splitNum,theBig,theSmalldef Split_Seq (seq): splitNum = seq [0] seq = seq [1:] # neither part contains the intermediate value So slice to remove seq [0] theBig = [x for x in seq if x > = splitNum] theSmall = [x for x in seq if x

< splitNum] return splitNum,theBig,theSmall#找出中间值def topKNum(seq,k): splitNum, theBig, theSmall = Split_Seq(seq) theBigLen = len(theBig) if k == theBigLen: return splitNum#出口,返回这个中间值, # 为什么不直接返回thebig?因为存在递归的原因thebig 不是在初始的seq找出来的 #需要重新Split,即可,读者自己思考 # 大值的列表中还未够K个数的情况, if k >

TheBigLen: return topKNum (theSmall,k-theBigLen-1) # cases where the number of large values is greater than K in the list return topKNum (theBig,k) # finds the TopK values from the intermediate values, def getTopK (seq,k): return [i for i in seq if i > topKNum (seq,k)] if _ _ name__ = ='_ _ main__': alist = [7,3,5,199885 234 2211 2211 2222 2211 2115] print ("= for verification Introduce sorted viewing = = ", sorted (alist,reverse= True)) print (getTopK (alist, 3)) the above is about" how to find the value and index of Topk in list by python ". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about related knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report