python 得到列表的第二大的元素

code

#coding=utf-8

l=[1,2,3,4,5,6,7]
max1=l[0]
max2=l[1]
if(max1>max2):
    pass
else:
    max1,max2=max2,max1

for i in l[2:]:
    if(i>max2):
        max2=i
    if(max2>max1):
        max1,max2=max2,max1
        
print(max2)

输出

 6

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/11284933.html