-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
list1 = [ {'name':'吴彦祖','age':'18','score':70,'tel':40508,'sex':'男'}, {'name':'彭于晏','age':'28','score':30,'tel':41008,'sex':'男'}, {'name':'成龙','age':'38','score':99,'tel':40999,'sex':'不明'}, {'name':'林宥嘉','age':'48','score':49,'tel':40081,'sex':'男'}, {'name':'周杰伦','age':'58','score':80,'tel':40082,'sex':'男'}, {'name':'吴京','age':'98','score':90,'tel':40084,'sex':'女'} ]
-
统计不及格学生的个数
count1 = 0 for i in list1: if i['score'] <= 60: count1 += 1 print(count1)
-
打印不及格学生的名字和对应的成绩
list2 = [] for i in list1: if i['score'] <= 60: list2.append(i['name']) list2.append(i['score']) print(list2)
-
打印手机尾号是8的学生的名字
for i in list1: if i['tel'] % 10 == 8: print(i['name'])
-
打印最高分和对应的学生的名字
max_count = 0 name = '' for i in list1: if i['score'] > max_count: max_count = i['score'] name = i['name'] print(name,max_count)
-
删除性别不明的所有学生
for i in list1: if i['sex'] == '不明': list1.remove(i) print(list1)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
-
-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
English = {'stu1','stu2','stu3','stu4','stu9'} Math = {'stu2','stu4','stu6','stu7','stu8'} Science = {'stu3','stu4','stu5','stu6','stu7','stu2'}
-
求选课学生总共有多少人
print('选课总人数为',len(English|Math|Science))
-
求只选了第一个学科的人的数量和对应的名字
nums = [] for i in (English-Math-Science): nums.append(i) print('学生名字:',nums,'学生数量为',len(English-Math-Science))
-
求只选了一门学科的学生的数量和对应的名字
nums = [] for i in (English - Math - Science): for j in (Math - Science - English): for k in (Science - English - Math): nums.append(i) nums.append(j) nums.append(k) print(i,j,k,len(nums))
-
求只选了两门学科的学生的数量和对应的名字
nums = [] for i in (English & Math): if i not in Science: nums.append(i) for j in (English & Science): if j not in Math: nums.append(j) for k in (Science & Math): if k not in English: nums.append(k) print('学生名字:',nums,'学生数量为',len(nums))
-
求选了三门学生的学生的数量和对应的名字
nums = [] for i in (English & Math & Science): nums.append(i) print('学生名字:', nums, '学生数量为', len(English & Math & Science))
-
day8-字典和集合作业
最新推荐文章于 2024-07-12 16:16:27 发布