删除python中列表中的最小数字

时间:2017-10-25 23:29:12

标签: python

我希望我的get_average函数将数字列表作为参数,从列表中删除最小数字,并计算平均数。该函数返回平均数。我是python中的一个初学者。我不明白如何从列表中删除最低数字。我真的卡住了。

我的计划到目前为止 -

def get_numbers():
    numbers = []
    num_to_get = int(input('How many numbers do you want to enter?'))
    while num_to_get > 0:
            num_to_get -= 1
            number = int(input('Enter a number: '))
            numbers.append(number)

    return numbers

def get_average(number_list, number):
    total = 0
    for number in number_list:
        total += number
    average = total/len(number_list)

    for i in range(len(number_list)):
        number_list[i] = 0
    number = 0

    return average

def show_average(value):
    print('Average:', value)

def main():
    numbers = get_numbers()
    print('Number List:', numbers)
    number = 123
    average = get_average(numbers, number)
    show_average(average)

main()

2 个答案:

答案 0 :(得分:0)

只需按顺序调用minremove方法:

seq = [2, 5, 3, -2, 7, 0, 19]
seq.remove(min(seq))
print seq

输出:

[2, 5, 3, 7, 0, 19]

min标识最低元素(-2)。 remove然后从列表中删除该值的第一个匹配项。

答案 1 :(得分:0)

def get_average(number_list):
    numbers = list(number_list)
    #remove smallest number
    numbers.remove(min(numbers))
    total = 0
    for number in numbers:
        total += number
    average = total/len(numbers)
    return average