练习题->求第二最值问题
问题:NESTED LISTS 如下图,链接:https://www.hackerrank.com/challenges/nested-list/problem
代码 :
1 if __name__ == '__main__': 2 dict = {} 3 dict2 = {} 4 for _ in range(int(input())): 5 name = input() 6 score = float(input()) 7 dict[name]=score 8 dict1 = dict.copy() 9 for key,value in dict1.items(): 10 if dict[key] == min(dict1.values()): 11 dict.pop(key) 12 for key,value in dict.items(): 13 if dict[key] == min(dict.values()): 14 dict2[key]=value 15 list1=sorted(dict2.keys()) 16 for i in list1: 17 print(i) 18
思路:像这种要求第二大或者第二小的问题,我都是习惯于先求出最大(小)的值,再把最值去掉,求剩余式的最值,基本只用到max,min函数
就像LeetCode的这道题,也是用同样的思路
谢谢!