欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

1.2 任意长度的可迭代对象分解为N个元素

程序员文章站 2022-05-28 13:10:16
...

解决方式:*表达式

grades = [56,45,26,89,35,98,54,39,54,80,54,62,61,60,97,95,90,61,37,65]

def drop_first_last(grades):
    first,*middle,last = grades
    return avg(middle)

def avg(grades):
    return sum(grades)/len(grades)

print(drop_first_last(grades))

输出:

63.166666666666664