• PTA的Python练习题(十二)-第4章-7 统计学生平均成绩与及格人数


    第4章-7 统计学生平均成绩与及格人数

    a=eval(input())
    b=list(map(int,input().split()))
    sum=sum(b)
    c=[i for i in b if i>=60]
    count=len(c)
    print('average = %.1f'%(sum/a))
    print('count = %d'%(count))

    如果这里用append输入只能实现一行一行输入,不能实现同一行输入

    知识点(列表的操作):

    
    """ ①实现嵌套列表的平铺 """
    vec = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    """ 第一个循环为外循环,第二个循环为内循环 """
    print([num for elem in vec for num in elem])
    
    print(list(chain(*vec)))
    
    """ 过滤不符合条件的元素 """
    alist = [-1, -4, 6, 7.5, -2.3, 9, -11]
    print([i for i in alist if i > 0])
    
    scores={"Zhang San":45, "Li Si":78, "Wang Wu":40, "Zhou Liu":96,
            "Zhao Qi":65, "Sun Ba":90, "Zheng Jiu":78, "Wu Shi":99,
            "Dong Shiyi":60}
    highest = max(scores.values())
    lowest = min(scores.values())
    average = sum(scores.values())/len(scores)
    print((highest, lowest, average))
    highestPerson = [name for name, score in scores.items() if score == highest]
    print(highestPerson)
    
    from random import randint
    x = [randint(1, 10) for i in range(20)]
    print(x)
    m = max(x)
    print([index for index, value in enumerate(x) if value == m])
    
    """ 同时遍历多个列表或可迭代对象 """
    print([(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y])
    print([(x, y) for x in [1, 2, 3] if x == 1 for y in [3, 1, 4] if y != x])
    
    """ 使用列表解析式实现矩阵转置 """
    matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
    print([[row[i] for row in matrix] for i in range(4)])
    
    print(list(map(list, zip(*matrix))))

     参考资料https://www.jianshu.com/p/da6e449f4c6a

    [Sign]做不出ctf题的时候很痛苦,你只能眼睁睁看着其他人领先你
  • 相关阅读:
    #QCon#北京2011大会“更有效地做测试”专题Slides资料
    长草了,冒个泡
    很好的一个书单
    团购——以价格换体验
    rpm deb命令集合[转]
    一个自动copy文件到指定目录的小程序
    ubuntulinux下的精品软件大汇总
    解决星际译王不朗读单词问题
    数据结构实验三:二叉树及其应用
    数据结构实验六:内部排序技术
  • 原文地址:https://www.cnblogs.com/echoDetected/p/12321462.html
Copyright © 2020-2023  润新知