Home  >  Article  >  Backend Development  >  How to average and sort integers in a list in python

How to average and sort integers in a list in python

高洛峰
高洛峰Original
2017-03-11 10:16:372322browse

This article mainly describes the process of using Python to average the key integers in the list and then arrange them, and shares the code with everyone. I hope everyone can applaud~~~

Question

Define a one-dimensional array of int type, containing 40 elements, used to store the results of each student, and loop to generate 40 random integers between 0 and 100.
(1) Store them in one dimension In the array, then count the number of students whose scores are lower than the average and output it.
(2) Output these 40 scores in order from high to low.

Solution (python)

#! /usr/bin python
#coding:utf-8
from __future__ import pision   #实现精确的除法,例如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)


The above is the detailed content of How to average and sort integers in a list in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn