##1.聲明一個(gè)字典保存一個(gè)學(xué)生的信息,學(xué)生信息中包括: 姓名、年齡、成績(jī)(單科)、電話、性別(男、女、不明)
dict1 = {'name': '瑞茲', 'age': 19, 'score': 90, 'phone':
19988880000, 'gender': 'man'}
list1 = [{'name': '瑞茲', 'age': 25, 'score': 95, 'phone': 19988880000, 'gender': 'man'},
{'name': '維恩', 'age': 16, 'score': 95, 'phone': 19988888888, 'gender': 'female'},
{'name': '寒冰', 'age': 15, 'score': 70, 'phone': 19988881111, 'gender': 'female'},
{'name': '龍女', 'age': 19, 'score': 80, 'phone': 19988882222, 'gender': 'female'},
{'name': '德萊文', 'age': 18, 'score': 50, 'phone': 19988883333, 'gender': 'unknown'},
{'name': '嘉文', 'age': 17, 'score': 59, 'phone': 19988885555, 'gender': 'man'}
]
1.統(tǒng)計(jì)不及格學(xué)生的個(gè)數(shù)
count = 0
for stu in list1:
if stu['score'] < 60:
count += 1
print(count)
2.打印不及格學(xué)生的名字和對(duì)應(yīng)的成績(jī)
for stu in list1:
if stu['score'] < 60:
print(stu['name'], stu['score'])
3.統(tǒng)計(jì)未成年學(xué)生的個(gè)數(shù)
for stu in list1:
if stu['age'] < 18:
count += 1
print('未成年學(xué)生個(gè)數(shù)為:', count)
4.打印手機(jī)尾號(hào)是8的學(xué)生的名字
for stu in list1:
if stu['phone'] % 10 == 8:
print('手機(jī)尾號(hào)是8的學(xué)生的名字是:', stu['name'])
5.打印最高分和對(duì)應(yīng)的學(xué)生的名字
max_score = 0
for stu in list1:
if stu['score'] > max_score:
max_score = stu['score']
for stu in list1:
if max_score == stu['score']:
print('最高分的學(xué)生為:', stu['name'])
7.刪除性別不明的所有學(xué)生
for stu in list1:
if stu['gender'] != 'man' and stu['gender'] != 'female':
list1.remove(stu)
print(list1)
3.用三個(gè)列表表示三門學(xué)科的選課學(xué)生姓名(一個(gè)學(xué)生可以同時(shí)選多門課)
chinese = ['寒冰', '嘉文', '瑞茲', '德萊文']
maths = ['希維爾', '瑞茲', '維恩', '寒冰']
english = ['希維爾', '德萊文', '瑞茲']
1.求選課學(xué)生總共有多少人
set1 = set(chinese)
set2 = set(maths)
set3 = set(english)
print('選課學(xué)生總共有%d人' % len(set1 | set2 | set3), '分別為:', set1 | set2 | set3)
2.求只選了第一個(gè)學(xué)科的人的數(shù)量和對(duì)應(yīng)的名字
print(len(set1 - set2 - set3), set1 - set2 - set3)
3.求只選了一門學(xué)科的學(xué)生的數(shù)量和對(duì)應(yīng)的名字
count = 0
x = ''
y = ''
z = ''
for stu in set1:
if stu not in set2 and set3:
x = x + ' ' + stu
count += 1
for stu in set2:
if stu not in set1 and set3:
print(stu)
y = y + ' ' + stu
count += 1
for stu in set3:
if stu not in set1 and set2:
z = z + ' ' + stu
count += 1
print('只選了一門學(xué)科的學(xué)生的數(shù)量為:', count, '名字是:', x, y, z)
4.求只選了兩門學(xué)科的學(xué)生的數(shù)量和對(duì)應(yīng)的名字
x = ''
y = ''
z = ''
count = 0
for stu in set1:
if stu in set2 and stu not in set3 or stu in set3 and stu not in set2:
x = x + ' ' + stu
count += 1
for stu in set2:
if stu in set1 and stu not in set3 or stu in set3 and stu not in set1:
y = y + ' ' + stu
count += 1
for stu in set3:
if stu in set1 and stu not in set2 or stu in set2 and stu not in set1:
z = z + ' ' + stu
count += 1
print(x, y, z, count)
5.求選了三門學(xué)生的學(xué)生的數(shù)量和對(duì)應(yīng)的名字
x = list(set(chinese) & set(maths) & set(english))
print(x)
print('===========================================================')
count = 0
for stu in chinese:
if stu in maths and stu in english:
print(stu)
for stu in maths:
if stu in chinese and stu in english:
print(stu)
for stu in english:
if stu in chinese and stu in maths:
print(stu)
count += 1
print(count)