python求平均数及打印出低于平均数的值列表


刚学Python的时候还是要多动手进行一些小程序的编写,要持续不断的进行,知识才能掌握的牢。今天就讲一下Python怎么求平均数,及打印出低于平均数的数值列表

 

 1 方法一:
 2 scores1 =  [91, 95, 97, 99, 92, 93, 96, 98]
 3 sum = 0
 4 scores2 = []
 5 for score in scores1:
 6     sum = sum + score
 7     average = sum/len(scores1)
 8 print('平均成绩是:{}'.format(average)) 
 9 
10 for score in scores1:
11     if score < average:
12         scores2.append(score) # 少于平均分的成绩放到新建的空列表中
13 print('低于平均成绩的有:{}'.format(scores2))  # format是格式化用的
14 
15 方法二:使用科学计算库的python 外接库--numpy,其中mean是库中求平均数用的,需要先在python中通过pip下载安装
16 import numpy as np
17 scores1 =  [91, 95, 97, 99, 92, 93, 96, 98]
18 scores2 = []
19 average = np.mean(scores1)  # mean意思求平均数的函数
20 print('平均成绩是:{}'.format(average))
21 
22 for score in scores1:
23     if score < average:
24         scores2.append(score) # 少于平均分的成绩放到新建的空列表中
25 print('低于平均成绩的有:{}'.format(scores2))  # format是格式化用的
26 
27 方法三:NumPy数组的操作,也可以打印出低于平均数的列表
28 scores3 = np.array(scores1)
29 print(' 低于平均成绩的有:{}'.format(scores3[scores3<average]))

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM