字典生成式:
案例一:
假设有20个学生,名字为westosx,学生分数在60-100之间,筛选出成绩 在90分以上的学生
第一种普通方法:
import random
stuInfo = {}
for i in range(20):
name = 'westos' + str(i)
score = random.randint(60,100)
stuInfo[name] = score
print(stuInfo)
highscore = {}
for name,score in stuInfo.items():
if score > 90:
highscore[name] = score
print(highscore)
第二种字典生成式:
import random
stuInfo = {}
for i in range(20):
name = 'westos' + str(i)
score = random.randint(60,100)