8-4 累計人數
1. 排序
#學生身高
heights = [120, 119, 125, 120, 120, 121, 123, 125, 119, 130, 125, 123, 120, 125, 120, 123]
#身高:人數
height_dict = {}
#逐一取出身高
for h in heights:
if h in height_dict: #如果身高已存在字典中
height_dict[h] += 1 #累加1人
else:
height_dict[h] = 1 #設定1人
#印出字典
print(height_dict){120: 5, 119: 2, 125: 4, 121: 1, 123: 3, 130: 1}自主練習
Last updated