0

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
Yared J.
  • 231
  • 1
  • 2
  • 9
  • 2
    You can order it and then access the highest, second highest, through `ordered_list[-1], ordered_list[-2]`etc. – Peter Wang Jul 29 '16 at 19:12
  • possible duplicate, http://stackoverflow.com/questions/33486058/python-how-to-find-the-second-highest-number-in-a-list – Rockybilly Jul 29 '16 at 19:14
  • Finding the `kth` largest element in an array is a pretty interesting problem. Sorting the list is a good bet, but there are `O(n)` average case solutions: http://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in-on – dashiell Jul 29 '16 at 19:20
  • @Rockybilly I don't believe this is a duplicate, as the original question did not request the index location of the nth highest number. – Alexander Jul 29 '16 at 19:26
  • @Yared What if the list is [10, 5, 5, 5]? What do you want as the return value(s)? – Alexander Jul 29 '16 at 19:28
  • I realized that my code wasn't what I needed to do. I edited to show what I'm really after – Yared J. Jul 29 '16 at 19:32
  • @YaredJ. 98 isn't the second highest value in the list. Do you want to get the second highest **absolute value**? – Chris Mueller Jul 29 '16 at 19:49

3 Answers3

1
>>> 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

joel goldstick
  • 4,393
  • 6
  • 30
  • 46
0

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
Chris Mueller
  • 6,490
  • 5
  • 29
  • 35
  • But the output I'm looking for is 98 from the other list [1,-3,5,2,6,11,78,5,-345,-3,6,98,-5,0] – Yared J. Jul 29 '16 at 19:44
  • @YaredJ. I've updated it to use that specific example, but the principle is the same. The key point in my answer is using `np.argsort` to get the indices of every sorted element. – Chris Mueller Jul 29 '16 at 19:46
0

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()
chapelo
  • 2,519
  • 13
  • 19