1.聲明一個字典保存一個學(xué)生的信息,學(xué)生信息中包括: 姓名、年齡、成績(單科)、電話
student = {'name': '張三', 'age': 23, 'score': 80, 'tel': '15382839992'}
2.聲明一個列表,在列表中保存6個學(xué)生的信息(6個題1中的字典)
all_student = [
{'name': '小明', 'age': 28, 'score': 89, 'tel': '15627382903'},
{'name': '路飛', 'age': 30, 'score': 59, 'tel': '15627382904'},
{'name': '鳴人', 'age': 29, 'score': 60, 'tel': '15627382908'},
{'name': '柯南', 'age': 17, 'score': 45, 'tel': '15627382909'},
{'name': '佐助', 'age': 22, 'score': 90, 'tel': '15627382902'},
{'name': '張楚嵐', 'age': 16, 'score': 90, 'tel': '15627382908'}
]
a.統(tǒng)計不及格學(xué)生的個數(shù)
count = 0
for stu in all_student:
if stu['score'] < 60:
count += 1
print('不及格的人數(shù): %d' % count)
b.打印不及格學(xué)生的名字和對應(yīng)的成績
for stu in all_student:
if stu['score'] < 60:
print(stu['name'], stu['score'])
c.統(tǒng)計未成年學(xué)生的個數(shù)
count = 0
for stu in all_student:
if stu['age'] < 18:
count += 1
print('未成年的人數(shù): %d' % count)
d.打印手機尾號是8的學(xué)生的名字
for stu in all_student:
tel = stu['tel']
if tel[-1] == '8':
print(stu['name'], tel)
e.打印最高分和對應(yīng)的學(xué)生的名字
max_score = 0
names = []
for stu in all_student:
score = stu['score']
if score >= max_score:
max_score = score
for stu in all_student:
if stu['score'] == max_score:
names.append(stu['name'])
print(names, max_score)
f.將列表按學(xué)生成績從大到小排序(掙扎一下,不行就放棄)
all_student = [
{'name': '小明', 'age': 28, 'score': 89, 'tel': '15627382903'},
{'name': '路飛', 'age': 30, 'score': 59, 'tel': '15627382904'},
{'name': '鳴人', 'age': 29, 'score': 60, 'tel': '15627382908'},
{'name': '柯南', 'age': 17, 'score': 45, 'tel': '15627382909'},
{'name': '佐助', 'age': 22, 'score': 90, 'tel': '15627382902'},
{'name': '張楚嵐', 'age': 16, 'score': 90, 'tel': '15627382908'}
]
# all_student = [
# {'name': '佐助', 'age': 22, 'score': 90, 'tel': '15627382902'},
# {'name': '張楚嵐', 'age': 16, 'score': 90, 'tel': '15627382908'},
# {'name': '小明', 'age': 28, 'score': 89, 'tel': '15627382903'},
# {'name': '鳴人', 'age': 29, 'score': 60, 'tel': '15627382908'},
# {'name': '路飛', 'age': 30, 'score': 59, 'tel': '15627382904'},
# {'name': '柯南', 'age': 17, 'score': 45, 'tel': '15627382909'}
# ]
# nums = [3, 2, 5, 7, 1]
"""
3, 2, 5, 7, 1
i = 0, j = 1,2,3,4
i = 0, j = 1: [3, 2, 5, 7, 1]
j = 2: [5, 2, 3, 7, 1]
j = 3: [7, 2, 3, 5, 1]
j = 4: [7, 2, 3, 5, 1]
i = 1, j = 2,3,4
j = 2: [7, 3, 2, 5, 1]
j = 3: [7, 5, 2, 3, 1]
j = 4: [7, 5, 2, 3, 1]
i = 2, j = 3, 4
j = 3: [7, 5, 3, 2, 1]
j = 4: [7, 5, 3, 2, 1]
i = 3, j = 4
j = 4: [7, 5, 3, 2, 1]
選擇排序: 讓前面的數(shù)依次和后面的每一個數(shù)比較大小, 如果后面的比前面的數(shù)大就交換位置(降序)。
整個過程執(zhí)行長度-1次
"""
選擇排序
nums = [3, 2, 5, 7, 1]
length = len(nums)
# i前面的數(shù)的下標(biāo)
for i in range(0, length-1):
# j表示前面數(shù)后邊的每個數(shù)的下標(biāo)
for j in range(i+1, length):
# 后面的數(shù)比前面的大
if nums[j] > nums[i]:
# 交換位置
nums[i], nums[j] = nums[j], nums[i]
print(nums)
length = len(all_student)
for i in range(0, length-1):
for j in range(i+1, length):
if all_student[j]['age'] > all_student[i]['age']:
all_student[i], all_student[j] = all_student[j], all_student[i]
print(all_student)
3.用三個列表表示三門學(xué)科的選課學(xué)生姓名(一個學(xué)生可以同時選多門課)
chinese = {'凜', '律', '鳴', '葉', '陸', '綠', '空', '白'}
math = {'凜', '律', '優(yōu)', '梓', '澪', '柚', '松', '柏'}
english = {'凜', '鳴', '紅', '唯', '澪', '空', '白', '堇'}
a. 求選課學(xué)生總共有多少人
all_student = chinese | math | english
print('選課學(xué)生的總?cè)藬?shù):', len(all_student), all_student)
b. 求只選了第一個學(xué)科的人的數(shù)量和對應(yīng)的名字
only_chinese = chinese - math - english
print('只選了語文的學(xué)生:', only_chinese)
c. 求只選了一門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
only_one = (chinese - math - english) | (math - chinese - english) | (english - math - chinese)
print('只選一門學(xué)科的學(xué)生', only_one)
only_one = (chinese ^ math ^ english) - (chinese & math & english)
print('只選一門學(xué)科的學(xué)生', only_one)
d. 求只選了兩門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
only_two = ((chinese & math) | (chinese & english) | (english & math)) - (chinese & math & english)
print('只選兩門學(xué)科的學(xué)生', only_two)
e. 求選了三門學(xué)生的學(xué)生的數(shù)量和對應(yīng)的名字
print(chinese & math & english)