python自定义一个数字列表,求列表中第二大数的下标

‘’’
自定义一个数字列表,求列表中第二大数的下标
‘’’

import random

number_list = []
for i in range(1, 11):
    ran = random.randint(1,30)
    number_list.append(ran)
    i += 1
print(number_list)

j = 1
max1 = number_list[0]
max2 = number_list[0]
while j < len(number_list):
    if number_list[j] > max1:
        max2 = max1
        max1 = number_list[j]
    else:
        if number_list[j] > max2:
            max2 = number_list[j]
    j += 1
c = [21, 54, 3, 5, 76, 32]
n = 0
while n < 2:
    i = 0
    max_num = c[0]
    while i < len(c):
        if c[i] > max_num:
            max_num = c[i]
        i+=1
    for j in range(len(c)):
        if c[j] == max_num:
            posi = j
            break
    del c[j]
    n += 1
print('第二大的数为:{},下标为:{}'.format(max_num,posi))

你可能感兴趣的:(python)