【问题标题】: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给定一个包含 X 个浮点数的列表,返回中间 3 个浮点数的平均值和该列表的最低浮点数的元组
【发布时间】:2020-07-17 14:21:23
【问题描述】:

例子:

输入:

[6.4, 11.4, 7.6, 10.5, 8.1]

预期输出:

(9.83, 6.4) 

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

【问题讨论】:

  • 你试过了吗?请与我们分享您失败的地方,以便我们为您提供帮助
  • 输出中的第一个元素应该是所有列表成员的平均值?
  • 欢迎来到 SO。这不是讨论论坛或教程。请使用tour 并花时间阅读How to Ask 以及该页面上的其他链接。花一些时间与the Tutorial 一起练习示例。它将让您了解 Python 提供的帮助您解决问题的工具。

标签: python python-3.x list mean min


【解决方案1】:

你可以试试这个。使用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 有一些例子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多