字典练习题

第一题

students = [
    {'name':'张三','age':18,'score':98,'tel':'18888888888','gender':'female'},
    {'name':'李四','age':20,'score':95,'tel':'18888888889','gender':'unkown'},
    {'name':'王五','age':18,'score':88,'tel':'18888888810','gender':'male'},
    {'name':'托尼','age':16,'score':58,'tel':'18888888811','gender':'unkown'},
    {'name':'李磊','age':19,'score':78,'tel':'18888888812','gender':'male'},
    {'name':'丽丽','age':17,'score':92,'tel':'18888888813','gender':'male'},
]

#(1)统计不及格学生的个数
#(2)打印不及格学生的名字和对应的成绩
#(3)统计未成年学生的个数
#(4)打印手机尾号是8的学生的名字
# (5) 打印最高分和对应学生的名字
#(6)删除性别不明的所有的学生
#(7)将列表按照学生成绩从大到小排序

#(1)
count = 0
age_count = 0
max_score = students[0]['score']
for student in students:
    if student['score'] < 60:
        count += 1
        print('不及格学生的名字:{},其对应的成绩:{}'.format(student['name'],student['score']))
    if student['age'] < 18:
        age_count += 1
    if student['tel'].endswith('8'):
        print('{}的手机尾号是以8结尾的'.format(student['name']))
    if student['score'] > max_score:
        max_score = student['score']

for student in students:
    if student['score'] == max_score:
        print('最高分是{},该分对应的学生是{}'.format(max_score,student['name']))

print('未成年学生的个数是%d' % age_count)
print('及格学生的个数为:',count)

new_students = [x for x in students if x['gender'] != 'unkown']
print(new_students)

new_students = filter(lambda x:x['gender'] != 'unkown',students)
print(list(new_students))

print('-------------------------------------')
# for j in range(0,len(students) - 1):
#     for i in range(0,len(students) -1 - j):
#         if students[i]['score'] > students[i + 1]['score']:
#             students[i],students[i + 1] = students[i+1],students[i]
# print(students)

sorted_students = sorted(students, key=lambda student : student['score'],reverse=True)
print(sorted_students)

第二题

# 用三个元组表示三门学科的选课学生姓名,(一个学生可以同时选多门课)
# (1)求选课学生总共有多少人
# (2) 求只选了第一个学科的人的数量和对应的名字
# (3) 求只选了一个学科的人的数量和对应的名字
# (4) 求只选了两门学科的学生的数量和对应的名字
# (5) 求选了三门学科的学生的数量和对应的名字


mathematics = ('小明','小红','小刚','小李','小熊','小雷','张三','李四')
chinese = ('小丽','小明','小红','小李','王三')
english = ('王五','小明','小刚','小熊','小雷')

students = []
student = mathematics + chinese + english

for i in student:
    if i not in students:
        students.append(i)
    else:
        pass

print('选课学生总共有{}人;他们分别是{}'.format(len(students),students))

first_only = []
for p in mathematics:
    if p not in chinese and p not in english:
        first_only.append(p)

print('只选择了第一个学科的人数是{},他们分别是{}'.format(len(first_only),first_only))

p_dict = {}
for name in student:
    if name not in p_dict:
        p_dict[name] = student.count(name)

print(p_dict)

only_one = []
only_two = []
only_three = []
for k,v in p_dict.items():
    if v == 1:
        only_one.append(k)
    if v == 2:
        only_two.append(k)
    if v == 3:
        only_three.append(k)

print('只选择了一门的学生数是{},分别是{}'.format(len(only_one),only_one))
print('只选择了二门的学生数是{},分别是{}'.format(len(only_two),only_two))
print('只选择了三门的学生数是{},分别是{}'.format(len(only_three),only_three))