给出X个浮点数的列表,返回中间3个浮点数和该列表中最低浮点数的平均值的元组

问题描述 投票:-1回答:1

示例:

输入:

[6.4, 11.4, 7.6, 10.5, 8.1]

预期输出:

(9.83, 6.4) 

9.83(四舍五入到小数点后两位)是平均值的11.4、7.6和10.5,而6.4是列表中的最低浮点数。

python python-3.x list mean min
1个回答
0
投票

您可以尝试这个。使用statistics.mean来获取中间3个元素的均值,然后使用round将它们四舍五入到两个位置,以最小使用min

from statistics import mean
a=[6.4, 11.4, 7.6, 10.5, 8.1]
mid=len(a)//2 - 1 #for extracting n/2th-1 position
out=(round(mean(a[mid:mid+3]),2),min(a))
# (9.83, 6.4)

注意:如果a为空,这将引发错误。

Demo带有一些示例。

© www.soinside.com 2019 - 2024. All rights reserved.