从各种数字列表中找出第二高的值

时间:2016-07-29 19:11:08

标签: python find

我制作了一个简单的代码,以便从各种数字列表中找到最高价值

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

但我怎么知道第二个,第三个等等?

我正在寻找类似的东西:

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

3 个答案:

答案 0 :(得分: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
>>> 

您可以对列表进行排序,然后使用倒数第二个值在列表中获得第二名

答案 1 :(得分:0)

您可以使用numpy来执行此操作。 np.argsort方法返回一个numpy索引数组,用于对列表进行排序。

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

如果你真正想要的是第二高的绝对值,那么你可以提前使用np.abs来获取列表的绝对值:

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

答案 2 :(得分:0)

尝试这种方法,将所有位置和排名映射到字典中:

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

您可以从此字典中访问名为mapping的任何值。它拥有您需要的所有信息: 键是(排名,列表号,位置)的tupples 并且值是单个值

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()