Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the 1st, 2nd, 3rd highest values in a list in Python

I know how to find the 1st highest value but don't know the rest. Keep in mind i need to print the position of the 1st 2nd and 3rd highest value.Thank You and try to keep it simple as i have only been coding for 2 months. Also they can be joint ranks

def linearSearch(Fscore_list):
    pos_list = []
    target = (max(Fscore_list))
    for i in range(len(Fscore_list)):
        if Fscore_list[i] >= target:
            pos_list.append(i)


        return pos_list
like image 916
Hashim Majeed Avatar asked Aug 30 '15 13:08

Hashim Majeed


People also ask

How do I find the 3rd highest number in a list in Python?

In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.

How do you find the nth largest number in a list in Python?

Algorithm. Step1: Input an integer list and the number of largest number. Step2: First traverse the list up to N times. Step3: Each traverse find the largest value and store it in a new list.


5 Answers

lst = [9,7,43,2,4,7,8,9,4]

temp1 = lst 
print(temp1)
    
#First Highest value: 
print(max(temp1))
temp1.remove(max(temp1))

#output: 43

# Second Highest value:
print(max(temp1))
temp1.remove(max(temp1))

#output: 9
    
        
# Third Highest Value:
print(max(temp1))

#output: 7
like image 127
Chandan Sharma Avatar answered Oct 04 '22 20:10

Chandan Sharma


This will create a list of the 3 largest items, and a list of the corresponding indices:

lst = [9,7,43,2,4,7,8,5,4]
values = []
values = zip(*sorted( [(x,i) for (i,x) in enumerate(f_test)], 
             reverse=True )[:3] )[0] 
posns = []
posns = zip(*sorted( [(x,i) for (i,x) in enumerate(f_test)], 
            reverse=True )[:3] )[1] 

Things are a bit more complicated if the same value can appear multiple times (this will show the highest position for a value):

lst = [9,7,43,2,4,7,8,5,4]
ranks = sorted( [(x,i) for (i,x) in enumerate(lst)], reverse=True 
)
values = []

for x,i in ranks:
    if x not in values:
        values.append( x )
        posns.append( i )
        if len(values) == 3:
            break
print zip( values, posns )
like image 32
Scott Hunter Avatar answered Oct 16 '22 21:10

Scott Hunter


Use heapq.nlargest:

>>> import heapq
>>> [i
...     for x, i
...     in heapq.nlargest(
...         3,
...         ((x, i) for i, x in enumerate((0,5,8,7,2,4,3,9,1))))]
[7, 2, 3]
like image 5
robert Avatar answered Oct 16 '22 19:10

robert


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.

Make sense?

Edit

thelist = [1, 45, 88, 1, 45, 88, 5, 2, 103, 103, 7, 8]
theset = frozenset(thelist)
theset = sorted(theset, reverse=True)

print('1st = ' + str(theset[0]) + ' at ' + str(thelist.index(theset[0])))
print('2nd = ' + str(theset[1]) + ' at ' + str(thelist.index(theset[1])))
print('3rd = ' + str(theset[2]) + ' at ' + str(thelist.index(theset[2])))

Edit

You still haven't told us how to handle 'joint winners' but looking at your responses to other answers I am guessing this might possibly be what you are trying to do, maybe? If this is not the output you want please give us an example of the output you are hoping to get.

thelist = [1, 45, 88, 1, 45, 88, 5, 2, 103, 103, 7, 8]
theset = frozenset(thelist)
theset = sorted(theset, reverse=True)
thedict = {}
for j in range(3):
    positions = [i for i, x in enumerate(thelist) if x == theset[j]]
    thedict[theset[j]] = positions

print('1st = ' + str(theset[0]) + ' at ' + str(thedict.get(theset[0])))
print('2nd = ' + str(theset[1]) + ' at ' + str(thedict.get(theset[1])))
print('3rd = ' + str(theset[2]) + ' at ' + str(thedict.get(theset[2])))

Output

1st = 103 at [8, 9]
2nd = 88 at [2, 5]
3rd = 45 at [1, 4]

BTW : What if all the values are the same (equal first) or for some other reason there is no third place? (or second place?). Do you need to protect against that? If you do then I'm sure you can work out appropriate safety shields to add to the code.

like image 1
jwpfox Avatar answered Oct 16 '22 19:10

jwpfox


Jupyter image of the code working This question was on my Udemy machine learning course way too soon. Scott Hunter helped me the most on this problem, but didn't get me to a pass on the site. Having to really think about the issue deeper on my own. Here is my solution, since couldn't find it anywhere else online--in terms that I understood everything that was going on*:

lst = [9,7,43,2,4,7,8,9,4]
ranks = sorted( [(x,i) for (i,x) in enumerate(lst)], reverse=True )
box = []
for x,i in ranks:
    if i&x not in box:
        box.append( x )
        if len(box) == 3:
            break
print(box)

So we have a list of numbers. To rank the numbers we sort the value with its position for every position that has a value when we enumerate/iterate the list. Then we put the highest values on top by reversing it. Now we need a box to put our information in to pull out of later, so we build that box []. Now for every value with a position put that in the box, if the value and position isn't already in the box--meaning if the value is already in the box, but the position isn't, still put in the box. And we only want three answers. Finally tell me what is in the variable called box. *Many of these answers, on this post, will most likely work.

like image 1
BrianBarnett Avatar answered Oct 16 '22 20:10

BrianBarnett