Python面试题8-找出列表里第二大的数
作者:互联网
第一种方法:通过排序,切片,取倒数第二个值
list1 = [1,2,6,4,5,7] list2 = sorted(list1) list3= list2[-2] print(list3)
第二种方法:循环列表中的值
list1 = [1,2,6,4,5,8,10,12,13,15] one = list1[0] two = list1[1] for i in range(1,len(list1)): if list1[i] > one: two = one one = list1[i] elif list1[i] > two: two = list1[i] else: pass print(two)
标签:面试题,Python,list1,two,list3,列表,print,list2 来源: https://www.cnblogs.com/all-best98/p/15322724.html