嗨,大家好!在這篇文章中,我們將看看在Python列表中找到平均值的各種方法。
一般來說,平均值是代表整個數據項或元素集的值。
公式:平均值=數字的總和/總計數。
在Python中找到列表平均值的技術
可以使用以下任一技術來計算Python列表的平均值:
- Python mean() 函數
- 內置 sum() 方法
- Python lambda 和 reduce() 方法
- Python operator.add() 方法
1. Python mean() 函數
Python 3具有statistics模塊
,其中包含用於計算數字的平均值或平均值的內置函數。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 List 平均值的各種技術。
參考資料
Source:
https://www.digitalocean.com/community/tutorials/average-of-list-in-python