代码块一:
#假设有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)
print({name: score for name,score in stuInfo.items() if score > 90})
示例一及运行结果:
代码块二: