इस लेख में, हम नीचे दिए गए समस्या कथन के समाधान के बारे में जानेंगे।
समस्या कथन - हमें दो पूर्णांक दिए गए हैं, हमें शब्दकोश में दूसरा अधिकतम मान प्रिंट करना होगा
आइए अब नीचे दिए गए कार्यान्वयन में अवधारणा को देखें-
दृष्टिकोण 1 - नकारात्मक अनुक्रमणिका द्वारा क्रमबद्ध() फ़ंक्शन का उपयोग करना
उदाहरण
#input
example_dict ={"tutor":3, "tutorials":15,
"point":9,"tutorialspoint":19}
# sorting the given list and get the second last element
print(list(sorted(example_dict.values()))[-2]) आउटपुट
15
दृष्टिकोण 2 - यहां हम सूची में सॉर्ट विधि का उपयोग करते हैं और फिर दूसरे सबसे बड़े तत्व तक पहुंचते हैं
उदाहरण
list1 = [11,22,1,2,5,67,21,32]
# using built-in sort method
list1.sort()
# second last element
print("Second largest element in the list is:", list1[-2]) आउटपुट
Second largest element in the list is: 32
दृष्टिकोण 3 - यहां हम एक अंतर्निहित फ़ंक्शन का उपयोग किए बिना ब्रूट-फोर्स विधि लागू करते हैं
उदाहरण
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
निष्कर्ष
इस लेख में, हमने सीखा है कि हम एक शब्दकोश में दूसरा अधिकतम मूल्य कैसे प्राप्त कर सकते हैं)।