Finding maximum, minimum and average in Python

Liu Rihui 2021-04-06 18:35:31
finding maximum minimum average python


One 、 subject

Answer the following questions in a loop :

1、 From the outside world 10 It's worth , Output Max 、 minimum value 、 Average ;
2、 From the outside world 10 It's worth , Index that outputs the maximum value 、 Index of minimum value .


Two 、 answer

Ideas : Make clear the knowledge needed by the topic first , Put it together again .

2.1 Getting data from the outside

input() function

There is no data type specified here , So we can take input() The returned string is converted to integer or floating point type , Such as : int(input(' Please enter an integer :\n'))  float(input(' Please enter a floating point number :\n'))

The data we get can be saved in a list . If the list is empty when it is created , You can add data continuously . such as :

a=[]

a.append(int(input()))

If the list wasn't empty , You can replace the elements in the list by the list name and index :

a=[1,2,3,4,5,6,7,8,9,10]

a[0]=int(input())

Because there are many elements , So we can do it in a circular way , such as :

i=0;

while i<10:

    a.append(int(input())) # If the list is empty at first

    a[i]=int(input())# If the list starts with data

    i=i+1

2.2 Get maximum

We can define a maximum value by ( The initial value is a number in the list ), Then compare the maximum value with all the elements in the list , If you find that there is a value greater than the maximum value , Let the maximum value be equal to the new maximum value / minimum value .

If you don't have to cycle , This is a :

theMax=a[0]

if a[1]>theMax:

    theMax=a[1]

if a[2]>theMax:

    theMax=a[2]

... In this way :

if(a[9]>theMax):

    theMax=a[9]

If you use a loop , We just need to make a change j Instead of the index of the list :

j=0;

while j<len(a):

    if j>theMax:

        theMax=j

    j++

Also, change the minimum value above to :

theMin=a[0]

if a[1]<theMin:

    theMin=a[1]

if a[2]<theMin:

    theMin=a[2]

... In this way :

if(a[9]<theMin):

    theMin=a[9]

If you use a loop , We just need to make a change j Instead of the index of the list :

j=0;

while j<len(a):

    if j<theMin:

        theMin=j

    j++

2.3 averaging

To find the average, first find the sum , The sum can be accumulated :

sum=a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]

Or in a circular way :

sum=0

for z in a:

    sum=sum+z

Then divide by the number of elements len(a) You can get the average .

theAvg=sum/len(a)

2.4 Find the best index

Declare two variables , The index used to store the maximum and minimum values , The default is zero ( The maximum value you set 、 Where does the minimum start , Which is the starting value of the corresponding maximum index ):

indexMax=0

indexMin=0

If it is to use for in : Cyclic , Get... With a temporary value :

indexMax=0
indexMin=0
for j in a:
if j>theMax:# If you find a value that's better than theMax Also big 
theMax=j
indexMax=a.index(j)# Get index through function 
if theMin>j:# If you find a value that's better than theMin Still small 
theMin=j
indexMin=a.index(j)# Get index through function 
theSum=theSum+j

It can also be used. a.index() Get the index in the same way :

q=0# Index of records 
for j in a:
if j>theMax:# If you find a value that's better than theMax Also big 
theMax=j
indexMax=q
if theMin>j:# If you find a value that's better than theMin Still small 
theMin=j
indexMin = q
theSum=theSum+j
q = q + 1

Or by while Way to obtain :

indexMax=0
indexMin=0
i=0# Index of records 
while i<len(a):
if a[i]>theMax:
theMax=a[i]
indexMax=i
if theMin>a[i]:
theMin=a[i]
indexMin=i
theSum=theSum+a[i]
i=i+1

3、 ... and 、 Code

Or use functions , But the original idea of this problem is to use circulation :

Be careful , although a.index(max(a)) You can get the index that appears , But this only returns the first occurrence of the maximum value

版权声明
本文为[Liu Rihui]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/04/20210406183354820G.html

  1. Spark Delta Lake 0.4.0 发布,支持 Python API 和部分 SQL
  2. How to transfer office files to PDF
  3. Are you still worried about multiple excel summary statistics? Python second processing really fragrant!
  4. Making music aggregate downloader with Python
  5. Spark delta Lake 0.4.0 is released, supporting Python API and part of SQL
  6. Python信息搜集
  7. Python information gathering
  8. Python - 关于类(self/cls) 以及 多进程通讯的思考
  9. Python - thinking about class (self / CLS) and multi process communication
  10. Python - 关于类(self/cls) 以及 多进程通讯的思考
  11. Python - thinking about class (self / CLS) and multi process communication
  12. Python信用评分卡建模(附代码)
  13. Python credit score card modeling (with code)
  14. 学Python需要学数据库吗?Python学习教程!
  15. Do you need to learn database to learn Python!
  16. Python私有变量如何定义?Python学习教程!
  17. How to define Python private variables? Python tutorial!
  18. Python数据分析入门(六):Pandas的函数应用
  19. Introduction to Python data analysis (6): function application of pandas
  20. 学Python需要学数据库吗?Python学习教程!
  21. Do you need to learn database to learn Python!
  22. Python描述 LeetCode 80. 删除有序数组中的重复项 II
  23. C++/python描述 AcWing 94. 递归实现排列型枚举
  24. C++/python描述 AcWing 92. 递归实现指数型枚举
  25. Python描述 LeetCode 88. 合并两个有序数组
  26. 苏州大学计算机考研 复试机试真题2013-2021真题及Python题解
  27. Python描述 LeetCode 781. 森林中的兔子
  28. 字典和json的区别是什么?Python学习
  29. Python describes leetcode 80. Removing duplicate items from ordered arrays II
  30. C + + / Python description acwing 94. Recursive implementation of permutation enumeration
  31. C + + / Python description acwing 92. Recursive implementation of exponential enumeration
  32. Python describes leetcode 88. Merging two ordered arrays
  33. Real computer test questions 2013-2021 of computer postgraduate entrance examination of Soochow University and python solutions
  34. The rabbit in the forest
  35. Python中的魔法属性
  36. What's the difference between dictionary and JSON? Python learning
  37. Magic properties in Python
  38. 字典和json的区别是什么?Python学习
  39. What's the difference between dictionary and JSON? Python learning
  40. python刷题-字母图形
  41. Python brush questions - letter graphics
  42. Python数据分析入门(七):Pandas层级索引
  43. Introduction to Python data analysis (7): Pandas hierarchical index
  44. Python 操作腾讯云短信(sms)详细教程
  45. Python operation Tencent cloud SMS (SMS) detailed tutorial
  46. Python数据可视化,完整版实操指南 !
  47. Python data visualization, full version of the practical guide!
  48. 上手Pandas,带你玩转数据(2)-- 使用pandas从多种文件中读取数据
  49. 上手Pandas,带你玩转数据(1)-- 实例详解pandas数据结构
  50. Using pandas to read data from various files
  51. Hands on pandas, take you to play with data (1) -- detailed explanation of pandas data structure with examples
  52. Pandas数据结构基础用法
  53. Basic usage of pandas data structure
  54. Python读取ini配置文件,保存到对象属性
  55. Python reads the INI configuration file and saves it to the object properties
  56. Foundation of Python: classes in Python
  57. python刷题-闰年判断
  58. python刷题-01字串
  59. How to judge leap year
  60. Python brush title-01 string