[英]Return the biggest but no more than a given float number using a given list of floats
[英]Given a list of X number of floats, return a tuple of the average of the middle 3 floats and the lowest float of that list
例子:
輸入:
[6.4, 11.4, 7.6, 10.5, 8.1]
預期 output:
(9.83, 6.4)
9.83(四舍五入到最接近的小數點后兩位)是 11.4、7.6 和 10.5 的平均值,6.4 是列表中的最低浮點數。
你可以試試這個。 使用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
為空,這將引發錯誤。
帶有一些示例的演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.