日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

day7-字典作业

發布時間:2024/3/12 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 day7-字典作业 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 定義一個列表,在列表中保存6個學生的信息(學生信息中包括: 姓名、年齡、成績(單科)、電話、性別(男、女、不明) )

  • 統計不及格學生的個數

    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
  • 打印不及格學生的名字和對應的成績

    for x in list1:score = x['score']name = x['name']if score < 60:print(name, score) #小明 24 小紅 54
  • 統計未成年學生的個數

    count = 0 for x in list1:age = x['age']if age < 18:count += 1 print(count) # 2
  • 打印手機尾號是8的學生的名字

    for x in list1:name = x['name']tel = int(x['tel'])if tel % 10 == 8:print(name, tel) # 小明 988
  • 打印最高分和對應的學生的名字

    max1 = 0 for x in list1:score = x['score']if score > max1:max1 = scorename = x['name'] print(max1,name) # 98 陳昕
  • 刪除性別不明的所有學生

    for x in list1:if x['gender'] == '不明':list1.remove(x) print(list1) 結果: [{'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': '男'}]
  • 將列表按學生成績從大到小排序(掙扎一下,不行就放棄)

  • 用三個元組表示三門學科的選課學生姓名(一個學生可以同時選多門課)

  • 求選課學生總共有多少人

    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('選修學生總人數:', len(new_stu6)) 結果:選修學生總人數: 9
  • 求只選了第一個學科的人的數量和對應的名字

    first = [] for stu in stu1:if stu not in stu2 and stu not in stu3:first.append(stu) print(first, len(first)) # ['李思', '陳來'] 2
  • 求只選了一門學科的學生的數量和對應的名字

    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)) 結果: ['李思', '陳來'] 2 ['小新', '小明'] 2 ['張四', '小名', '小紅', '小張'] 4
  • 求只選了兩門學科的學生的數量和對應的名字(有問題)

    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) # ['陳來']
  • 求選了三門學生的學生的數量和對應的名字

  • stu1 = ('張三', '李思', '陳來') stu2 = ('張三', '小新', '小明', '陳來') stu3 = ('張四', '小名', '小紅', '小張', '張三') for x in stu5:if( x in stu1 and x in stu2 and x in stu3):stu6 += [x] print(stu6) # ['張三'] 3

    總結

    以上是生活随笔為你收集整理的day7-字典作业的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。