0
Find second largest number in list ?------need detailed explanation in this code
n=int(input()) list1=list(map(int,input().split(" ")[:n])) list1.sort() c=list1.count(max(list1)) r=list1[len(list1)-(c+1)] print(r)
4 Respostas
+ 8
NAVEEN VYAS ,
n=int(input()) # define the number of values to input
list1=list(map(int,input().split(" ")[:n])) # input the desired number of values separated by a space. this creates a list of the input numbers as integer values
list1.sort() # sort the list inplace in ascending order
c=list1.count(max(list1)) # count how often the max value exists in the list
r=list1[len(list1)-(c+1)] # pick the second largest number by using a calculated index
print(r) # print the second largest number
+ 1
The ...[:n] in line 2
is used to specify the number of elements to return in list created using split.
0
Then y [:n] in second line
0
What it indicates