嗨伙计!在本文中,我们将了解在Python 列表** 中查找列表平均值的各种**方法。
通常,平均值是代表一整套数据项或元素的值。
公式:平均值u003d数字总和/总数。
Python中查找列表平均值的技巧
以下任何一种技术都可用于计算 Python 中列表的平均值/平均值:
-
Python mean() 函数
-
内置 sum() 方法
-
Python lambda 和 reduce() 方法
-
Python operator.add() 方法
1. Python均值()函数
Python 3 具有statistics module
,其中包含一个内置函数来计算数字的平均值或平均值。statistics.mean() function
用于计算输入值或数据集的平均值/平均值。
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() 函数
Pythonstatistics.sum()
函数也可用于查找 Python 列表中数据值的平均值。
statistics.len()
函数用于计算列表的长度,即列表中存在的数据项的计数。
句法:
len(input-list)
此外,statistics.sum()
函数用于计算列表中所有数据项的总和。
句法:
sum(input-list)
注意:平均值 u003d(总和)/(计数)。
例子:
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() 函数。
Python reduce() 函数:reduce() function
基本上用于将特定(输入)函数应用于传递给函数的元素集。
句法:
reduce(function,input-list/sequence)
-
最初,reduce() 函数将传递的函数应用于前两个连续元素并返回结果。
-
此外,我们将相同的函数应用于上一步中获得的结果以及第二个元素之后的元素。
-
这个过程一直持续到列表的末尾。
-
最后将结果作为输出返回到终端/屏幕。
Python lambda() 函数:lambda() function
用于构建和形成匿名函数即没有名称或签名的函数。
句法:
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
zoz100036 * *
4. Python operator.add() 函数查找列表的平均值
Python 运算符模块 包含各种函数,可以有效地执行基本计算和操作。
operator.add()
函数可用于在 Python reduce() 函数的帮助下计算列表中所有数据值的总和。
句法:
operator.add(value1, value2)
注意:平均值 u003d(总和)/(元素的长度或计数)
例子:
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 列表的平均值。
参考文献
-
NumPy average() 方法 - 官方文档
-
算子模块 - 官方文档
-
Python NumPy 模块
-
Python 列表
更多推荐