首页 > 技术文章 > python学习--列表操作实例1

hayden1106 2017-12-11 13:56 原文

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
知识点:
1. 随机数的批量生成
2. 列表元素值的判断,及计算
'''

from __future__ import division
'''
导入python未来支持的语言特征division(精确除法),当我们没有在程序中导入
该特征时,"/"操作符执行的是截断除法(Truncating Division),当我们导入精确
除法之后,"/"执行的是精确除法,如下所示:
'''
import random
#生成0-100之间的40个随机数
score = [random.randint(0,100) for i in range(40)]
print(score)
num = len(score)
sum_score = sum(score)

ave_num = sum_score/num

#列表中小于平均值的个数
less_ave = len([i for i in score if i<ave_num])
print("平均值是:{}".format(ave_num))
print("有 {} 名学生的成绩小于平均值。".format(less_ave))

#排序
'''sorted(iterable, /, *, key=None, reverse=False)
    Return a new list containing all items from the iterable in ascending order.
    
    A custom key function can be supplied to customize the sort order, and the
    reverse flag can be set to request the result in descending order.
    https://www.cnblogs.com/kellyseeme/p/5525052.html
    http://blog.csdn.net/zyl1042635242/article/details/43115675
'''
sorted_score = sorted(score, reverse=True)
print(sorted_score)

 

 

推荐阅读