在Python中找到列表的平均值的5种方法

嗨,大家好!在这篇文章中,我们将看一下在Python列表中查找列表平均值的各种方法

通常,平均值是代表整个数据项或元素集的值。

公式:平均值=数字的总和/总数。


在Python中查找列表平均值的技巧

以下任一技巧都可用于计算Python列表的平均值:

  • Python mean()函数
  • 内置sum()方法
  • Python lambda和reduce()方法
  • Python operator.add()方法

1. Python mean()函数

Python 3statistics模块,其中包含一个用于计算数字的平均值或平均值的内置函数。 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)

注意:平均值 =(总和)/(计数)

示例:

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() 函数 基本上是用于将特定(输入)函数应用于传递给函数的一组元素。

语法:

reduce(function,input-list/sequence)
  • 最初,reduce() 函数将传递的函数应用于前两个连续元素,并返回结果。
  • 接下来,我们将相同的函数应用于前一步骤中获得的结果和后续第二个元素。
  • 此过程持续进行直到达到列表的末尾。
  • 最后,结果作为输出返回到终端/屏幕。

Python lambda() 函数 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 operator 模块 包含各种函数,可高效执行基本计算和操作。

operator.add() 函数可利用 Python reduce() 函数 计算列表中所有数据值的总和。

语法:

operator.add(value1, value2)

注意:平均值 = (总和)/(元素长度或数量)

示例:

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列表平均值的各种技巧。


参考资料

Source:
https://www.digitalocean.com/community/tutorials/average-of-list-in-python