如何对后续文档测试进行排序、切片并返回平均值?python

2024-04-19 19:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

def score(numbers):
    """Return  the average of the numbers excluding the biggest and the smallest one"""


score([2, 7, 9, 10, 13, 1, 5, 12])  # Answer must be =7.5
score([3, 7, 2.5, -4)]              # Answer must be 2.75

请帮忙。你知道吗


Tags: andoftheanswerreturndefbeone
2条回答
def score(l):
    l = sorted(l)
    return sum(l[1:-1]) / float(len(l) - 2)

print(score([2, 7, 9, 10, 13, 1, 5, 12]))
print(score([3, 7, 2.5, -4]))
 >>> def score(a):
...   a.remove(max(a))
...   a.remove(min(a))
...   print sum(a)/(float(len(a)))

另外,还有一种方法可以得到列表的平均值,你也可以使用这个方法。你知道吗

print reduce(lambda x, y: x + y / float(len(a)), a, 0)

相关问题 更多 >