WebAug 30, 2015 · Add all the values in the list to a set. This will ensure you have each value only once. Sort the set. Find the index of the top three values in the set in the original list. WebOct 21, 2010 · >>> list (enumerate (a)) [ (0, 3), (1, 2), (2, 1), (3, 4), (4, 5)] At this point, we can extract the position of max with >>> max (enumerate (a), key= (lambda x: x [1])) (4, 5) The above tells us, the maximum is in the position 4 and his value is 5.
How to find the 1st, 2nd, 3rd highest values in a list in Python
WebUse max (): Using itemgetter (): In [53]: lis= [ (101, 153), (255, 827), (361, 961)] In [81]: from operator import itemgetter In [82]: max (lis,key=itemgetter (1)) [0] #faster solution Out [82]: 361 using lambda: In [54]: max (lis,key=lambda item:item [1]) Out [54]: (361, 961) In [55]: max (lis,key=lambda item:item [1]) [0] Out [55]: 361 WebMar 28, 2024 · Method 2: Using the inbuilt function. Python’s inbuilt function allows us to find it in one line, we can find the minimum in the list using the min () function and then use the index () function to find out the index of that minimum element. Similarly we can do the same for finding out the maximum element using the max () function and then ... chin strap beard with long goatee
python - How do I index the 3 highest values in a list? - Stack Overflow
WebThis approach is without using max () function a = [1,2,3,4,6,7,99,88,999] max_num = 0 for i in a: if i > max_num: max_num = i print (max_num) Also if you want to find the index of the resulting max, print (a.index (max_num)) Direct approach by using function max () WebSep 24, 2024 · from operator import itemgetter my_list = [3.5, 1.6, 2.4, 5.6, 8.9] result = [p [1] for p in sorted (sorted (enumerate (my_list), key = itemgetter (1)) [-3:], key = itemgetter (0))] print (result) Output: [3.5, 5.6, 8.9] Share Improve this answer Follow edited Sep 23, 2024 at 21:34 answered Sep 23, 2024 at 21:23 DjaouadNM 21.8k 4 32 54 Webnumpy.argmax. #. Returns the indices of the maximum values along an axis. Input array. By default, the index is into the flattened array, otherwise along the specified axis. If provided, the result will be inserted into this array. It should be of the appropriate shape and dtype. If this is set to True, the axes which are reduced are left in ... granny the game for windows 10