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

posted @ 2020-02-17 13:44  ch0bits  阅读(3061)  评论(0编辑  收藏  举报