python里对list中的整数求平均并排序
2019/7/13 21:45:59
本文主要是介绍python里对list中的整数求平均并排序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
问题
定义一个int型的一维数组,包含40个元素,用来存储每个学员的成绩,循环产生40个0~100之间的随机整数,
(1)将它们存储到一维数组中,然后统计成绩低于平均分的学员的人数,并输出出来。
(2)将这40个成绩按照从高到低的顺序输出出来。
解决(python)
#! /usr/bin python #coding:utf-8 from __future__ import division #实现精确的除法,例如4/3=1.333333 import random def make_score(num): score = [random.randint(0,100) for i in range(num)] return score def less_average(score): num = len(score) sum_score = sum(score) ave_num = sum_score/num less_ave = [i for i in score if i<ave_num] return len(less_ave) if __name__=="__main__": score = make_score(40) print "the number of less average is:",less_average(score) print "the every socre is[from big to small]:",sorted(score,reverse=True)
这篇关于python里对list中的整数求平均并排序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python