用 Python 求列表平均值的 5 种方法

大家好!在本文中,我们将介绍在Python列表中查找列表平均值的各种 方法。

一般而言,平均值是表示整个数据项或元素集的值。

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


在Python中求列表平均值的技巧

可以使用以下两种技术之一来计算Python中列表的平均值:

  • PythonMean()函数
  • 内置sum()方法
  • Python lambda and Reduce()方法
  • Python运算符.add()方法

1.Python均值()函数

Python3统计模块,其中内置了一个函数来计算数字的平均值或平均值。函数的作用是:计算输入值或数据集的** 平均值/平均值** 。

Mean()函数 接受包含数值的列表、元组或数据集作为参数,并返回数据项的平均值。

语法:

1mean(data-set/input-values)

示例:

1from statistics import mean 
2
3inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88] 
4list_avg = mean(inp_lst) 
5
6print("Average value of the list:\n") 
7print(list_avg) 
8print("Average value of the list with precision upto 3 decimal value:\n")
9print(round(list_avg,3))

在上面的代码片段中,我们使用了Statistics tics.round()方法将输出平均值向上舍入到一个特定的小数值

语法:

1statistics.round(value, precision value)

输出:

1Average value of the list:
2
367.51375
4Average value of the list with precision upto 3 decimal value:
5
667.514

2.使用Python sum()函数

也可以使用PythonStatistics tics.sum()函数来查找PythonList中数据值的平均值。

函数的作用是:计算列表的长度,即列表中存在的数据项的数量。

语法:

1len(input-list)

此外,使用Statistics tics.sum()函数来计算列表中所有数据项的总和。

语法:

1sum(input-list)

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

示例:

 1from statistics import mean 
 2
 3inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
 4
 5sum_lst = sum(inp_lst)
 6
 7lst_avg = sum_lst/len(inp_lst)
 8print("Average value of the list:\n") 
 9print(lst_avg) 
10print("Average value of the list with precision upto 3 decimal value:\n")
11print(round(lst_avg,3))

输出:

1Average value of the list:
2
367.51375
4Average value of the list with precision upto 3 decimal value:
5
667.514

3.使用PythonReduct()和lambda方法

我们可以结合lambda() 函数和** PythonReduce()** 函数一起使用。

PYTHON REDUTE()函数REDUTE()函数主要用于将特定的(输入)函数应用于传递给该函数的元素集。

语法:

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

Python lambda()function lambda()函数用于构建和形成匿名functions,即没有名称和签名的函数。

语法:

1lambda arguments:function

示例:

 1from functools import reduce 
 2
 3inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
 4
 5lst_len= len(inp_lst)
 6
 7lst_avg = reduce(lambda x, y: x + y, inp_lst) /lst_len 
 8print("Average value of the list:\n") 
 9print(lst_avg) 
10print("Average value of the list with precision upto 3 decimal value:\n")
11print(round(lst_avg,3))

输出:

1Average value of the list:
2
367.51375
4Average value of the list with precision upto 3 decimal value:
5
667.514

4. Python operator.add()函数用于查找列表的平均值

Python运算符模块 包含各种函数,可以高效地执行基本的计算和操作。

通过PythonReduce()函数 ,可以使用OPERATOR.ADD()函数计算列表中所有数据值的总和。

语法:

1operator.add(value1, value2)

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

示例:

 1from functools import reduce 
 2import operator
 3inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
 4
 5lst_len = len(inp_lst)
 6
 7lst_avg = reduce(operator.add, inp_lst) /lst_len 
 8print("Average value of the list:\n") 
 9print(lst_avg) 
10print("Average value of the list with precision upto 3 decimal value:\n")
11print(round(lst_avg,3))

输出:

1Average value of the list:
2
367.51375
4Average value of the list with precision upto 3 decimal value:
5
667.514

5.在Python中计算列表平均值的NumPy Average()方法

PythonNumPy module]有一个内置的函数来计算数据集或列表中存在的数据项的平均值。

计算输入列表的平均值时,使用numpy.verage()方法。

示例:

1import numpy
2
3inp_lst = [12, 45, 78, 36, 45, 237.11, -1, 88]
4
5lst_avg = numpy.average(inp_lst)
6print("Average value of the list:\n") 
7print(lst_avg) 
8print("Average value of the list with precision upto 3 decimal value:\n")
9print(round(lst_avg,3))

输出

1Average value of the list:
2
367.51375
4Average value of the list with precision upto 3 decimal value:
5
667.514

结论

因此,在这篇文章中,我们介绍并理解了各种求出Python列表平均值的技术。


参考文献

Published At
Categories with 技术
Tagged with
comments powered by Disqus