5种方法得出 Python 列表的平均值
作者 Safa Mulani
在这篇文章中,我们将讨论在 Python 列表中寻找平均值的各种方法。通常来说,平均值是一个反映整组数据项或元素一般水平的值。 公式:平均值 = 数字的总和 / 总数。
在 Python 列表中找到平均值的方法
以下任何一种方法都可以用来计算 Python 列表的平均值:
- Python mean() 函数
- 内置的 sum() 方法
- Python lambda 和 reduce() 方法
- Python operator.add() 方法
1. Python mean() 函数
Python 3 有统计模块(statistics module
),它包含一个内置的函数来计算数字的平均值。statistics.mean()
函数用于计算输入值或数据集的平均值。
mean()
函数接受包含数字值的列表、元组或数据集作为形参,并返回数据项的平均值。
句法:
mean(data-set/input-values)
示例:
from statistics import mean
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
list_avg = mean(inp_lst)
print("Average value of the list:\n")
print(list_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(list_avg,3))
在上面的代码片段中,我们使用了 statistics.round()
方法,将输出的平均值四舍五入到一个特定的小数点。
句法:
statistics.round(value, precision value)
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
2. 使用Python sum()函数
Python statistics.sum()
函数也可以用来寻找 Python 列表中数据值的平均值。
statistics.len()
函数用于计算列表的长度,即列表中存在的数据项的数量。
句法:
len(input-list)
此外,statistics.sum()
函数还用于计算列表中所有数据项的总和。
句法:
sum(input-list)
注意:平均值 = (sum) / (数量) 示例:
from statistics import mean
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
sum_lst = sum(inp_lst)
lst_avg = sum_lst/len(inp_lst)
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
3. 使用Python reduce() 和 lambda 方法
我们可以将 Python reduce()函数与 lambda()函数一起使用。
reduce()
函数基本上用于将一个特定的(输入)函数应用于传递给该函数的元素集合。
句法:
reduce(function,input-list/sequence)
步骤是:
- 首先,reduce()函数将传递的函数应用于前两个连续元素,并返回结果。
- 然后,我们对上一步得到的结果和第二个元素之后的元素应用同样的函数。
- 以上过程一直持续到列表的末尾。
- 最后,将结果作为输出返回到终端/屏幕。
lambda()
函数用于建立和形成匿名函数,即没有名称或签名的函数。 句法:
lambda arguments:function
示例:
from functools import reduce
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
lst_len= len(inp_lst)
lst_avg = reduce(lambda x, y: x + y, inp_lst) /lst_len
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
4. 用 Python operator.add()函数查找列表的平均值
Python 运算符模块包含各种函数,可以有效地进行基本计算和操作。
在Python reduce()函数的帮助下,operator.add()
函数可以用来计算列表中存在的所有数据值的总和。
句法:
operator.add(value1, value2)
注意:平均数 = (sum) / (元素的长度或数量) 示例:
from functools import reduce
import operator
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
lst_len = len(inp_lst)
lst_avg = reduce(operator.add, inp_lst) /lst_len
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
5. 用 NumPy average()方法计算 Python 列表的平均值
Python 的 NumPy 模块有一个内置的函数来计算数据集或列表中数据项的平均值,即numpy.average()
方法。
示例
import numpy
inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
lst_avg = numpy.average(inp_lst)
print("Average value of the list:\n")
print(lst_avg)
print("Average value of the list with precision upto 3 decimal value:\n")
print(round(lst_avg,3))
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
结语
在这篇文章中,我们探讨并理解了寻找 Python 列表平均值的各种方法。