字典(dict)作业
生活随笔
收集整理的這篇文章主要介紹了
字典(dict)作业
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
定義一個變量保存一個學生的信息,學生信息中包括:姓名、年齡、成績(單科)、電話、性別
student={'name':'小明', 'age':18, 'score':89, 'telephone':'18782620878', 'gender':'男'}定義一個列表,在列表中保存6個學生的信息(學生信息中包括: 姓名、年齡、成績(單科)、電話、性別(男、女、不明) )
student=[{'name':'A', 'age':19, 'score':67, 'telephone':'18782620878', 'gender':'男'},{'name':'B', 'age':20, 'score':89, 'telephone':'18782620870', 'gender':'女'},{'name':'C', 'age':17, 'score':59, 'telephone':'18782622878', 'gender':'不明'},{'name':'D', 'age':16, 'score':99, 'telephone':'18782620880', 'gender':'女'},{'name':'E', 'age':18, 'score':55, 'telephone':'18782621878', 'gender':'不明'},{'name':'F', 'age':19, 'score':99, 'telephone':'18782620800', 'gender':'男'},]統計不及格學生的個數
count = 0 for i in student:count += 1 if i['score']<60 else 0 print(count)打印不及格未成年學生的名字和對應的成績
for i in student:if i['score'] < 60 and i['age'] < 18:print('不及格未成年學生 姓名:{} 成績{}'.format(i['name'], i['score']))求所有男生的平均年齡
count = 0 ages = 0 for i in student:if i['gender'] == '男':count += 1ages += i['age'] else:print('男生的平均年齡:',ages/count)打印手機尾號是8的學生的名字
names=[] for i in student:names += i['name'] if i['telephone'][-1] == '8' else [] print('手機尾號是8的學生的名字:',names)打印最高分和對應的學生的名字
name_score=[student[0]['name'],student[0]['score']] for i in student[1:]:if i['score'] > name_score[-1]:name_score=[i['name'],i['score']]elif i['score'] == name_score[-1]:name_score.insert(0,i['name']) print('最高分{};對應的學生的名字{}:'.format(name_score[-1],name_score[-2::-1]))刪除性別不明的所有學生
for i in range(len(student)-1,-1,-1):if student[i]['gender'] == '不明':print(student.pop(i))將列表按學生成績從大到小排序(掙扎一下,不行就放棄)
# 1 sorts=[student[0]] for i in student[1:]:for j in range(len(sorts)):if i['score'] > sorts[j]['score']:sorts.insert(j,i)breakelse:sorts.append(i) print(sorts)# 2 student.sort(key=lambda x: x['score'], reverse=True) print(student)定義一個變量保存一個班級的信息,班級信息中包括:班級名稱、教室位置、班主任信息、講師信息、班級所有的學生(根據實際情況確定數據類型和具體信息)
Class = {'Class_name': 'python2107','Classroom': '18教','head_teacher': {'name':'A', 'age':35, 'telephone':'18782620878', 'gender':'女'},'Instructor': [{'name':'B', 'age':36, 'telephone':'18782620878', 'gender':'男'}, {}, ...],'students': [{'name':'C', 'age':18, 'score':89, 'telephone':'18782620878', 'gender':'男'}, {}, ...] }已知一個列表保存了多個狗對應的字典:
dogs = [{'name': '貝貝', 'color': '白色', 'breed': '銀狐', 'age': 3, 'gender': '母'},{'name': '花花', 'color': '灰色', 'breed': '法斗', 'age': 2},{'name': '財財', 'color': '黑色', 'breed': '土狗', 'age': 5, 'gender': '公'},{'name': '包子', 'color': '黃色', 'breed': '哈士奇', 'age': 1},{'name': '可樂', 'color': '白色', 'breed': '銀狐', 'age': 2},{'name': '旺財', 'color': '黃色', 'breed': '土狗', 'age': 2, 'gender': '母'} ]利用列表推導式獲取所有狗的品種
[‘銀狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘銀狐’, ‘土狗’]
[i['breed'] for i in dogs]利用列表推導式獲取所有白色狗的名字
[‘貝貝’, ‘可樂’]
[i['name'] for i in dogs if i['color'] == '白色']給dogs中沒有性別的狗添加性別為 ‘公’
for i in range(len(dogs)):dogs[i] = dogs[i].setdefault('gender', '公') print(dogs)統計 ‘銀狐’ 的數量
count = 0 for i in dogs:count += 1 if i['breed'] == '銀狐' else 0 print('銀狐的個數:', count)總結
以上是生活随笔為你收集整理的字典(dict)作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 优雅的定时器
- 下一篇: BGP路由黑洞解决办法介绍