在 Python 中查找 Numpy 数组列表的平均值
pythonserver side programmingprogramming更新于 2025/10/11 15:08:17
Numpy 是一个非常强大的 Python 数值数据处理库。它主要以数组的形式接收数据,并应用各种函数(包括统计函数)从数组中获取结果。在本文中,我们将了解如何获取给定数组的平均值。
使用平均值
平均值函数可以接收数组并给出其中所有元素的数学平均值。因此,我们设计了一个 for 循环来跟踪输入的长度,并遍历每个数组计算其平均值。
示例
import numpy as np
# GIven Array
Arrays_In = [np.array([11, 5, 41]),
np.array([12, 13, 26]),
np.array([56, 20, 51])]
# 结果数组
Arrays_res = []
# 使用 np.mean()
for x in range(len(Arrays_In)):
Arrays_res.append(np.mean(Arrays_In[x]))
# 结果
print("The means of the arrays: \n",Arrays_res)
输出
运行上述代码得到以下结果 −
The means of the arrays: [19.0, 17.0, 42.333333333333336]
使用 Average
除了我们使用 average 函数而不是 mean 函数外,它与上面的方法非常相似。它给出的结果完全相同。
示例
import numpy as np
# GIven Array
Arrays_In = [np.array([11, 5, 41]),
np.array([12, 13, 26]),
np.array([56, 20, 51])]
# Resultihg Array
Arrays_res = []
# 使用 np.average()
for x in range(len(Arrays_In)):
Arrays_res.append(np.average(Arrays_In[x]))
# 结果
print("The means of the arrays: \n",Arrays_res)
输出
运行上述代码得到以下结果 −
The means of the arrays: [19.0, 17.0, 42.333333333333336]
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

