python列表查找值
Hi Folks! In this article, we will have a look at the various ways to find the average of a list in a Python List.
嗨伙计! 在本文中,我们将介绍在Python List中查找列表平均值的各种方法 。
In general, an average is a value that represents a whole set of data items or elements.
通常,平均值是代表整个数据项或元素集的值。
Formula: Average = summation of numbers/total count.
公式:平均值=数字总和/总计数。
Either of the following techniques can be used to calculate the average/mean of a list in Python:
以下任何一种技术均可用于计算Python中列表的平均值/均值:
Python 3 has statistics module
which contains an in-built function to calculate the mean or average of numbers. The statistics.mean() function
is used to calculate the mean/average of input values or data set.
Python 3具有statistics module
,该statistics module
包含一个内置函数来计算数字的均值或平均值。 statistics.mean() function
用于计算输入值或数据集的平均值/平均值 。
The mean() function accepts the list, tuple or data-set containing numeric values as a parameter and returns the average of the data-items.
mean()函数接受包含数值的列表,元组或数据集作为参数,并返回数据项的平均值。
Syntax:
句法:
mean(data-set/input-values)
Example:
例:
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))
In the above snippet of code, we have used statistics.round()
method to round off the output average up to a particular decimal value.
在上面的代码片段中,我们使用了statistics.round()
方法将输出平均值四舍五入到特定的十进制值 。
Syntax:
句法:
statistics.round(value, precision value)
Output:
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
Python statistics.sum()
function can also be used to find the average of data values in Python list.
Python statistics.sum()
函数还可用于在Python列表中查找数据值的平均值。
The statistics.len()
function is used to calculate the length of the list i.e. the count of data items present in the list.
statistics.len()
函数用于计算列表的长度,即列表中存在的数据项的数量。
Syntax:
句法:
len(input-list)
Further, statistics.sum()
function is used to calculate the sum of all the data items in the list.
此外, statistics.sum()
函数用于计算列表中所有数据项的总和。
Syntax:
句法:
sum(input-list)
Note: average = (sum)/(count).
注意: average =(sum)/(count) 。
Example:
例:
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))
Output:
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
We can use Python reduce() function along with the lambda() function.
我们可以将Python reduce()函数与lambda()函数一起使用。
Python reduce() function: The reduce() function
is basically used to apply a particular(input) function to the set of elements passed to the function.
Python reduce()函数 : reduce() function
基本上用于将特定(输入)函数应用于传递给该函数的元素集。
Syntax:
句法:
reduce(function,input-list/sequence)
Python lambda() function: The lambda() function
is used to build and form Anonymous functions i.e. function without a name or signature.
Python lambda()函数 : lambda() function
用于构建和形成匿名函数,即没有名称或签名的函数。
Syntax:
句法:
lambda arguments:function
Example:
例:
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))
Output:
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
The Python operator module contains various functions to perform basic calculations and operations efficiently.
Python运算符模块包含各种功能,可以有效地执行基本计算和操作。
The operator.add()
function can be used to calculate the summation of all the data values present in the list with the help of Python reduce() function.
借助Python reduce()函数,可以使用operator.add()
函数来计算列表中存在的所有数据值的总和。
Syntax:
句法:
operator.add(value1, value2)
Note: average = (sum)/(length or count of elements)
注意:平均值=(总和)/(元素的长度或数量)
Example:
例:
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))
Output:
输出:
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
Python’s NumPy module has an in-built function to calculate the average/mean of the data items present in the data set or list.
Python的NumPy模块具有一个内置函数,用于计算数据集或列表中存在的数据项的平均值/均值。
The numpy.average()
method is used to calculate the average of the input list.
numpy.average()
方法用于计算输入列表的平均值。
Example:
例:
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))
Output:
输出 :
Average value of the list:
67.51375
Average value of the list with precision upto 3 decimal value:
67.514
Thus, in this article, we have unveiled and understood various techniques to find the average of a Python List.
因此,在本文中,我们揭示并理解了各种技术来查找Python列表的平均值。
翻译自: https://www.journaldev.com/37498/average-of-list-in-python
python列表查找值