1.聲明一個字典保存一個學(xué)生的信息衔肢,學(xué)生信息中包括: 姓名碧绞、年齡从藤、成績(單科)、電話
student={'姓名':'張三','年齡':14,'成績':89,'電話':2314}
print(student)
2.聲明一個列表守屉,在列表中保存6個學(xué)生的信息(6個題1中的字典)
a.統(tǒng)計不及格學(xué)生的個數(shù)
b.打印不及格學(xué)生的名字和對應(yīng)的成績
c.統(tǒng)計未成年學(xué)生的個數(shù)
d.打印手機尾號是8的學(xué)生的名字
e.打印最高分和對應(yīng)的學(xué)生的名字
f.將列表按學(xué)生成績從大到小排序(掙扎一下惑艇,不行就放棄)
all_student = [
{'name':'張一','age':18,'score':95,'Tel':'4359'},
{'name':'張二','age':19,'score':86,'Tel':'1434'},
{'name':'張三','age':16,'score':41,'Tel':'4355'},
{'name':'張四','age':18,'score':56,'Tel':'1543'},
{'name':'張五','age':20,'score':77,'Tel':'9505'},
{'name':'張六','age':17,'score':89,'Tel':'0845'}
]
count=0#不及格人數(shù)
count1=0#未成年人數(shù)
for stu in all_student:
if stu['score']<60:
print('%s:%d' % (stu['name'],stu['score']))
count+=1
if stu['age']<18:
count1+=1
if stu['Tel'][-1]=='8':
print('%s的電話'%(stu['name'],stu['Tel']))
print('不及格人數(shù):',count)
print('未成年人數(shù):',count)
max_score=0
for stu in all_student:
if stu['score']>max_score:
max_score=stu['score']
for stu in all_student:
if stu['score']==max_score:
print(stu['name'],max_score)
a=[]
b=[]
for stu in all_student:
a.append(stu['score'])
a.sort(reverse=True)
for x in a:
for y in all_student:
if x==y['score']:
b.append(y)
print(b)
3.用三個列表表示三門學(xué)科的選課學(xué)生姓名(一個學(xué)生可以同時選多門課)
a. 求選課學(xué)生總共有多少人
b. 求只選了第一個學(xué)科的人的數(shù)量和對應(yīng)的名字
c. 求只選了一門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
d. 求只選了兩門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
e. 求選了三門學(xué)生的學(xué)生的數(shù)量和對應(yīng)的名字
names1 = ['name1', 'name2', 'name3', 'name4', 'name5', 'name6']
names2 = ['name1', 'name2', 'name7', 'name8', 'name9', 'name10']
names3 = ['name2', 'name3', 'name4', 'name7', 'name11', 'name12']
tota = set(names1) | set(names2) | set(names3)
print('選課學(xué)生總共有多少人:%d' % len(tota))
total = set(names1) - (set(names2) | set(names3))
print('只選了第一個學(xué)科的人的數(shù)量:%d,對應(yīng)的名字:%s' % (len(total), str(total)[1:-1]))
#奇數(shù)個補集要多減一個交集
total1 = (set(names1) ^ set(names2) ^ set(names3)) - (set(names1) & set(names2) & set(names3))
print('只選了一門學(xué)科的學(xué)生的數(shù)量:%d, 對應(yīng)的名字:%s' % (len(total1), str(total1)[1:-1]))
#所有學(xué)生 - 只選了一科的學(xué)生 - 選了三科的學(xué)生
total2 = tota - total1 - (set(names1) & set(names2) & set(names3))
print('只選了兩門學(xué)科的學(xué)生的數(shù)量:%d, 對應(yīng)的名字:%s' % (len(total2), str(total2)[1:-1]))
total3 = set(names1) & set(names2) & set(names3)
print('選了三門學(xué)科的學(xué)生的數(shù)量:%d, 對應(yīng)的名字:%s' % (len(total3), str(total3)[1:-1]))