统计考试成绩中优秀,良,中,及格,不及格的人数。
方法一
scores=[89,70,49,87,92,84,73,71,78,81,90,37,
77,82,81,79,80,82,75,90,54,80,70,68,61]
groups ={'优秀':0,'良':0,'中':0,'及格':0,'不及格':0}
for score in scores:
if score >= 90:
groups['优秀']=groups['优秀']+1
elif score>=80:
groups['良']=groups['良']+1
elif score>=70:
groups['中']=groups['中']+1
elif score>=60:
groups['及格']=groups['及格']+1
else:
groups['不及格']=groups['不及格']+1
print(groups)
方法二
from itertools import group by
scores=[89,70,49,87,92,84,73,71,78,81,90,37,
77,82,81,79,80,82,75,90,54,80,70,68,61]
def classify(score):
if score >= 90:
return '优秀'
elif score >= 80:
return '良'
elif score >= 70:
return '中'
elif score >= 60:
return '及格'
else:
return '不及格'
groups={category:len(tuple(score))for category,score in groupby(sorted(scores),classify)}
print(groups)
方法三
from collections import Counter
from pandas import cut
scores=[89,70,49,87,92,84,73,71,78,81,90,37,
77,82,81,79,80,82,75,90,54,80,70,68,61]
groups = Counter(cut(scores,[0, 60, 78, 80, 90, 101],
labels=['不及格','及格','中','良','优秀'],
right=False))
print(groups)