Yared J.
Yared J.

Reputation: 241

Find the second highest value out of various lists of numbers

I made a simple code to find the highest value out of various lists of numbers

lists = [[1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0],[1,2,3,4,5,6,7,6,5,4,4],[-435,-64,-4,-6,-45,-8,-98,-7,-8],[32,45,56,554,12,33]]
for w in lists:
    lst = w
    a = float ("-inf")
    for x in range (0, len (lst)):
        b = lst [x]
        if (b > a):
            a = b
            c = x
            z = lst
print ("The list is:",z)
print ("The highest value is: " , a)
print ("The position is:", c+1)

Out:
The list is: [32, 45, 56, 554, 12, 33]
The highest value is:  554
The position is: 4

But how can I know the second, third and so on??

I'm looking for something like this:

Out:
The list is: [1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0] 
The second highest value is: 98
The position is: 12

Upvotes: 0

Views: 689

Answers (3)

chapelo
chapelo

Reputation: 2562

Try this approach, that maps all the positions and rankings in a dictionary:

from operator import itemgetter

lists = [[1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0],
         [1,2,3,4,5,6,7,6,5,4,4],
         [-435,-64,-4,-6,-45,-8,-98,-7,-8],
         [32,45,56,554,12,33]]

rank = 0
mapping = {(rank, lst_no, pos): val 
               for lst_no, lst in enumerate(lists)
               for pos, val in enumerate(lst)}

value = float('nan')
rank_incr = 0
for (_, lst_no, pos), val in sorted(
                      temp.items(), reverse=True, key=itemgetter(1)):
    # The following section is to assign the same rank
    # to repeated values, and continue counting thereafter.
    if val != value:             
        value = val
        rank += rank_incr
        rank_incr = 1
    else:
        rank_incr += 1
    # -----------------
    del mapping((0, lst_no, pos))
    mapping[(rank, lst_no, pos)] = val

You can access any value from this dictionary named mapping. It has all the information you need: the keys are tupples of ( rank, list no, position ) and the values are the individual values

for (rank, lst_no, pos), val in sorted(mapping.items()):
    print("Ranking No. {}".format(rank))
    print("    The value: {}".format(val))
    print("    The list No. {}, is: {}".format(lst_no, lists[lst_no]))
    print("    The position is: {}".format(pos))
    print()

Upvotes: 0

Chris Mueller
Chris Mueller

Reputation: 6680

You can use numpy to do this. The np.argsort method returns a numpy array of the indices which would sort the list.

>>> import numpy as np
>>> list = [1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0]
>>> inds = np.argsort(list)
>>> print('The highest value is: {0}'.format(list[inds[-1]]))
The highest value is: 98
>>> print('Second highest value is: {0}'.format(list[inds[-2]]))
Second highest value is: 78
>>> print('Third highest value is: {0}'.format(list[inds[-3]]))
Third highest value is: 11

If what you actually want is the second highest absolute value, then you can simply take the absolute value of the list using np.abs ahead of time:

>>> import numpy as np
>>> list = [1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0]
>>> inds = np.argsort(np.abs(list))
>>> print('The highest absolute value is: {0}'.format(list[inds[-1]]))
The highest absolute value is: -345
>>> print('Second highest absolute value is: {0}'.format(list[inds[-2]]))
Second highest absolute value is: 98
>>> print('Third highest absolute value is: {0}'.format(list[inds[-3]]))
Third highest absolute value is: 78

Upvotes: 0

joel goldstick
joel goldstick

Reputation: 4493

>>> lst = [1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0]

>>> sorted(lst)
[-345, -5, -3, -3, 0, 1, 2, 5, 5, 6, 6, 11, 78, 98]
>>> 
>>> second_most = sorted(lst)[-2]
>>> second_most
78
>>> 
>>> lst.index(78)
6
>>> 

You can sort the list, then take the second to last value to get second most in the list

Upvotes: 1

Related Questions