>  기사  >  백엔드 개발  >  python里对list中的整数求平均并排序

python里对list中的整数求平均并排序

WBOY
WBOY원래의
2016-06-06 11:32:181176검색

问题

定义一个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)

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.