day7-字典作业
定義一個列表,在列表中保存6個學(xué)生的信息(學(xué)生信息中包括: 姓名、年齡、成績(單科)、電話、性別(男、女、不明) )
統(tǒng)計不及格學(xué)生的個數(shù)
list1 = [{'name': '晨晨', 'age':18, 'score': 78, 'tel': '123', 'gender': '男'},{'name': '陳來', 'age':20, 'score': 80, 'tel': '321', 'gender': '不明'},{'name': '陳昕', 'age':28, 'score': 98, 'tel': '653', 'gender': '女'},{'name': '小新', 'age':32, 'score': 65, 'tel': '783', 'gender': '男'},{'name': '小明', 'age':17, 'score': 24, 'tel': '988', 'gender': '女'},{'name': '小紅', 'age':14, 'score': 54, 'tel': '903', 'gender': '男'} ] count = 0 for x in list1:score = x['score']if score < 60:count += 1 print(count) # 2打印不及格學(xué)生的名字和對應(yīng)的成績
for x in list1:score = x['score']name = x['name']if score < 60:print(name, score) #小明 24 小紅 54統(tǒng)計未成年學(xué)生的個數(shù)
count = 0 for x in list1:age = x['age']if age < 18:count += 1 print(count) # 2打印手機尾號是8的學(xué)生的名字
for x in list1:name = x['name']tel = int(x['tel'])if tel % 10 == 8:print(name, tel) # 小明 988打印最高分和對應(yīng)的學(xué)生的名字
max1 = 0 for x in list1:score = x['score']if score > max1:max1 = scorename = x['name'] print(max1,name) # 98 陳昕刪除性別不明的所有學(xué)生
for x in list1:if x['gender'] == '不明':list1.remove(x) print(list1) 結(jié)果: [{'name': '晨晨', 'age': 18, 'score': 78, 'tel': '123', 'gender': '男'}, {'name': '陳昕', 'age': 28, 'score': 98, 'tel': '653', 'gender': '女'}, {'name': '小新', 'age': 32, 'score': 65, 'tel': '783', 'gender': '男'}, {'name': '小明', 'age': 17, 'score': 24, 'tel': '988', 'gender': '女'}, {'name': '小紅', 'age': 14, 'score': 54, 'tel': '903', 'gender': '男'}]將列表按學(xué)生成績從大到小排序(掙扎一下,不行就放棄)
用三個元組表示三門學(xué)科的選課學(xué)生姓名(一個學(xué)生可以同時選多門課)
求選課學(xué)生總共有多少人
stu1 = ('張三', '李思', '陳來') stu2 = ('張三', '小新', '小明') stu3 = ('張四', '小名', '小紅', '小張') stu4 = () new_stu4 = [x for x in stu1 if x not in stu4] new_stu5 = new_stu4 + [y for y in stu2 if y not in new_stu4] new_stu6 = new_stu5 + [z for z in stu3 if z not in new_stu5] print('選修學(xué)生總?cè)藬?shù):', len(new_stu6)) 結(jié)果:選修學(xué)生總?cè)藬?shù): 9求只選了第一個學(xué)科的人的數(shù)量和對應(yīng)的名字
first = [] for stu in stu1:if stu not in stu2 and stu not in stu3:first.append(stu) print(first, len(first)) # ['李思', '陳來'] 2求只選了一門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字
stu1 = ('張三', '李思', '陳來') stu2 = ('張三', '小新', '小明') stu3 = ('張四', '小名', '小紅', '小張', '張三') math1 = [] english1 = [] history1 = [] for x in stu1:if x not in stu2 and x not in stu3:math1.append(x) print(math1, len(math1)) english1 = [y for y in stu2 if (y not in stu1 and y not in stu3)] print(english1, len(english1)) history1 = [z for z in stu3 if (z not in stu1 and z not in stu2)] print(history1, len(history1)) 結(jié)果: ['李思', '陳來'] 2 ['小新', '小明'] 2 ['張四', '小名', '小紅', '小張'] 4求只選了兩門學(xué)科的學(xué)生的數(shù)量和對應(yīng)的名字(有問題)
stu1 = ('張三', '李思', '陳來') stu2 = ('張三', '小新', '小明', '陳來') stu3 = ('張四', '小名', '小紅', '小張', '張三') stu5 = ['張三', '李思', '陳來', '小新', '小明', '張四', '小名', '小紅', '小張'] stu6 = [] count = 0 for x in stu5:if (x in stu1 and x in stu2 and x not in stu3) or (x in stu2 and x in stu3 and x not in stu1) or (x in stu1 and x in stu3 and x not in stu2):stu6 += [x] print(stu6) # ['陳來']求選了三門學(xué)生的學(xué)生的數(shù)量和對應(yīng)的名字
總結(jié)
- 上一篇: 特征选择: 卡方检验、F 检验和互信息
- 下一篇: cin.tie() 输入加速器