在本文中,我们将学习下面给出的问题陈述的解决方案。
问题陈述 -我们给了两个整数,我们需要在字典中打印第二个最大值
现在让我们观察一下下面的实现中的概念-
sorted()
通过负索引使用函数#input example_dict ={"tutor":3, "tutorials":15, "point":9,"nhooo":19} # sorting the given list and get the second last element print(list(sorted(example_dict.values()))[-2])
15
list1 = [11,22,1,2,5,67,21,32] # using built-in sort method list1.sort() # second last element print("列表中的第二大元素是:", list1[-2])
列表中的第二大元素是: 32
list1 = [11,22,1,2,5,67,21,32] #assuming max_ is equal to maximum of element at 0th and 1st index and secondmax is the minimum among them max_=max(list1[0],list1[1]) secondmax=min(list1[0],list1[1]) for i in range(2,len(list1)): # if found element is greater than max_ if list1[i]>max_: secondmax=max_ max_=list1[i] #if found element is greator than secondmax else: if list1[i]>secondmax: secondmax=list1[i] print("Second highest number is the list is : ",str(secondmax))
Second highest number is the list is : 32
在本文中,我们了解了如何在字典中找到第二个最大值。