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.

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

在Python中查找列表平均值的技巧 (Techniques to find the average of a list in Python)

Either of the following techniques can be used to calculate the average/mean of a list in Python:

以下任何一种技术均可用于计算Python中列表的平均值/均值:

Python mean() function

Python mean()函数

In-built sum() method

内置sum()方法

Python lambda and reduce() method

Python Lambda和reduce()方法

Python operator.add() method

Python operator.add()方法

1. Python mean()函数 (1. Python mean() function)

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

2.使用Python sum()函数 (2. Using Python sum() function)

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

3.使用Python reduce()和lambda方法 (3. Using Python reduce() and lambda method)

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)

Initially, the reduce() function applies the passed function to the first two consecutive elements and returns the result.

最初,reduce()函数将传递的函数应用于前两个连续的元素,并返回结果。

Further, we apply the same function to the result obtained in the previous step and the element succeeding the second element.

此外,我们将相同的函数应用于上一步中获得的结果以及第二个元素之后的元素。

This process continues until it reaches the end of the list.

此过程一直持续到列表末尾。

Finally, the result is returned to the terminal/screen as output.

最后,结果作为输出返回到终端/屏幕。

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

4. Python operator.add()函数查找列表的平均值 (4. Python operator.add() function to find the average of a list)

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

5. NumPy average()方法来计算Python中列表的平均值 (5. NumPy average() method to calculate the average of a list in Python)

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

结论 (Conclusion)

Thus, in this article, we have unveiled and understood various techniques to find the average of a Python List.

因此,在本文中,我们揭示并理解了各种技术来查找Python列表的平均值。

参考资料 (References)

NumPy average() method – Official Documentation

NumPy average()方法–官方文档

The operator module – Official Documentation

操作员模块–官方文档

Python NumPy module

Python NumPy模块

Python List

Python清单

翻译自: https://www.journaldev.com/37498/average-of-list-in-python

python列表查找值

python从键盘输入一个列表计算输出元素的平均值_python列表查找值_在Python中查找列表平均值的5种方法...相关推荐

  1. 【问题描述】3.1.5 用整数1~7依次表示星期一至星期日。由键盘输入一个整数,输出对应的英文表示,如果输入的整数在1~7之外,输出“Error”信息。【样例输入1】1【样例输出1】Monda

    [问题描述]3.1.5 用整数1~7依次表示星期一至星期日.由键盘输入一个整数,输出对应的英文表示,如果输入的整数在1~7之外,输出"Error"信息. [样例输入1]1 [样例输 ...

  2. Java学习之编写实现简单加密的程序,要求从键盘输入一个字符,输出加密后的字符。

    案例介绍: 编写实现简单加密的程序,要求从键盘输入一个字符,输出加密后的字符. 加密规则:输入A,输出Z;输入B,输出Y:输入a,输出z;输入b,输出y. 案例代码: import java.io.* ...

  3. C语言 编一程序,从键盘输入一个实数,输出其绝对值。

    编一程序,从键盘输入一个实数,输出其绝对值. 常规方法: #include<stdio.h> main() {float fx;printf("please input a fl ...

  4. java程序a-z b-y,请完成下列Java程序:对大写的26个英文字母加密,从键盘输入一个大写字母串,输出这个串加密后的结 - 赏学吧...

    请完成下列Java程序:对大写的26个英文字母加密,从键盘输入一个大写字母串,输出这个串加密后的结果.加密操作是将字母变换成倒序的大写字母,如A->Z,B->Y. 注意:请勿改动main( ...

  5. 从键盘输入一个字符串并输出

    import java.util.Scanner; //导入 java.util 包下的 Scanner 类 class ScannerTest { //定义一个ScannerTest类public ...

  6. python从键盘输入一个字符串、将小写字母全部_从键盘输入一个字符串_将其中的小写字母全部转换成大写字母...

    从键盘输入一个字符串, 将其中的小写字母全部转换成大写字母, 然后输出到一个磁盘文件 "test" 中保存.输入的字符串以 " ! " 结束 . 我写的程序是 ...

  7. python从键盘输入一个字符串、将小写字母全部_从键盘输入一个字符串,将其中的小写字母全部转换成大写字母...

    从键盘输入一个字符串, 将其中的小写字母全部转换成大写字母, 然后输出到一个磁盘文件 "test" 中保存.输入的字符串以 " ! " 结束 . 我写的程序是 ...

  8. [转载] python输入一个年份、输出是否为闰年_Python程序检查给定年份是否为闰年

    参考链接: Python输入,输出和导入 闰年是除世纪年(以00结尾的一年)外完全可以被4整除的一年.如果一个世纪可以被400整除,那它就是闰年.在这里,一年是由用户提供的,我们必须检查给定的年份是否 ...

  9. C语言用整数1~12依次表示1~12月,由键盘输入一个月份,输出对应的季节英文名称

    #include<stdio.h> main() {int i,j;printf("请输入一个月份");scanf("%d",&i);swi ...

最新文章

  1. linux按文件名排序ls,linux – 如何使用shell脚本按名称对文件进行排序
  2. linux date时间戳互相转换
  3. CTFshow 命令执行 web119
  4. Codeforces 987A. Infinity Gauntlet(手速题,map存一下输出即可)
  5. 通过jquery-ui中的sortable来实现拖拽排序
  6. oracle课程小结,Oracle 数据库优化实战心得总结
  7. vss2005管理vs2010项目
  8. macos必做的设置_如何在MacOS上设置PHP,CaddyServer和Kirby —以及为什么要这样做
  9. 登峰连接程式改坐标软件_数控仿真软件CIMCOEdit常用的操作,新手必看
  10. 什么是 DNS 劫持、投毒、解析?看这文就懂了!
  11. debian6安装后中文乱码
  12. 解决父类加载iframe,src参数过大导致加载失败
  13. E - 权势二进制 哈尔滨理工大学软件学院大一个人赛训练
  14. 最新教程:M1芯片的Mac电脑进入恢复模式?
  15. 电能表软件测试用例,一种用于电能表软件自动测试的方法专利_专利查询 - 天眼查...
  16. pandas求协方差、相关系数、显著性检验
  17. IEEE会议Latex模板下载教程
  18. 【java实现控制台打印表格】
  19. 数据可视化第八章使用matplotlib绘制高级图表
  20. 神经网络量化入门--基本原理

热门文章

  1. 【STM32】1—零基础硬件软件配置 完成LED的闪烁
  2. win7不能访问本地网络共享的问题解决(错误代码:0x800704cf )
  3. 2016年下半年信息系统项目管理师真题及答案_信息系统项目管理师历年真题及权威答案_信息系统项目管理师试题及模拟题_软考考试题库_希赛网...
  4. 用 Python 制作GUI钢琴~几行代码就能完成一个游戏项目
  5. 安卓android BMI体质指数测试项目完整版
  6. 【百度之星2020】Mosquito 解题报告
  7. java美食网站设计与实现_毕业设计 基于JAVA的美食娱乐分享网站的设计与实现
  8. Navicat导入excel的xlsx文件提示无法打开文件
  9. photoshop制作气泡
  10. [翻译]C#中的泛型 (From dotNet SDK 2.0 Beta1)