获取不同条目平均高度的 Python 程序
pythonserver side programmingprogramming更新于 2025/12/21 10:52:17
假设我们有一组高度,其中可能也有一些重复的条目。我们必须找到这些高度的不同条目的平均值。
因此,如果输入为 heights = [96,25,83,96,33,83,24,25],则输出将为 52.2,因为唯一元素为 [96,25,83,33,24],因此总和为 96 + 25 + 83 + 33 + 24 = 261,平均值为 261/5 = 52.2。
为了解决这个问题,我们将遵循以下步骤 −
h_set := a set from heights to remove duplicates
返回 h_set 项目的总和 / h_set 集合的大小
示例
让我们看看下面的实现以便更好地理解
def solve(heights): h_set = set(heights) return sum(h_set)/len(h_set) heights = [96,25,83,96,33,83,24,25] print(solve(heights))
输入
[96,25,83,96,33,83,24,25]
输出
52.2
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

