实际案例:统计分数范围
假设我们有一组学生的分数,我们希望统计在不同分数范围内的学生人数。
scores = (85, 92, 78, 95, 88, 60, 72, 98, 83, 75)
count_low = sum(1 for score in scores if score < 70)
count_medium = sum(1 for score in scores if 70 <= score < 85)
count_high = sum(1 for score in scores if score >= 85)
print(f"低分学生人数:{count_low}")
print(f"中等分数学生人数:{count_medium}")
print(f"高分学生人数:{count_high}")
1 scores = (85, 92, 78, 95, 88, 60, 72, 98, 83, 75) 2 count_low = sum(1 for score in scores if score < 70) 3 count_medium = sum(1 for score in scores if 70 <= score < 85) 4 count_high = sum(1 for score in scores if score >= 85) 5 print(f"低分学生人数:{count_low}") 6 print(f"中等分数学生人数:{count_medium}") 7 print(f"高分学生人数:{count_high}")
这个案例中,我们使用了元组的切片和迭代功能,结合列表解析,快速统计了不同分数范围内的学生人数。
标签:count,分数,元组,案例,score,scores,print,学生 From: https://www.cnblogs.com/szmtjs10/p/18095226