day7-列表和字典作业
生活随笔
收集整理的這篇文章主要介紹了
day7-列表和字典作业
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.創建一個列表,列表中有10個舒宗, 保證列表中元素的順序,對列表進行排重,并對列表使用進行降序排序
例如:隨機生成了[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]--- 去重之后 [70, 88, 91, 107, 234, 177, 282, 197]---- 降序排序 [282, 234, 197, 177, 107, 91, 88, 70]?```python nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197] new_nums = [] for item in nums:if item not in new_nums:new_nums.append(item) new_nums.sort(reverse=True) print(new_nums) 2.利用列表推導式, 完成以下需求a. 生成一個存放1-100中各位數為3的數據列表結果為 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
?```python list1 = [x * 10 + 3 for x in range(10)] print(list1)b. 利用列表推到是將 列表中的整數提取出來
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21] scores = [True, 17, "hello", "bye", 98, 34, 21] nums = [x for x in scores if type(x) == int] print(nums)c.利用列表推導式 存放指定列表中字符串的長度
例如 ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3] list1 = ["good", "nice", "see you", "bye"] list2 = [len(item) for item in list1] print(list2)4.已經一個班級字典如下:
class1 = {'name': 'python2104','address': '23教','lecturer': {'name': '余婷', 'age': 18, 'QQ': '726550822'},'leader': {'name': '舒玲', 'age': 18, 'QQ': '2343844', 'tel': '110'},'students': [{'name': 'stu1', 'school': '清華大學', 'tel': '1123', 'age': 18, 'score': 98, 'linkman': {'name': '張三', 'tel': '923'}},{'name': 'stu2', 'school': '攀枝花學院', 'tel': '8999', 'age': 28, 'score': 76, 'linkman': {'name': '李四', 'tel': '902'}},{'name': 'stu3', 'school': '成都理工大學', 'tel': '678', 'age': 20, 'score': 53, 'linkman': {'name': '小明', 'tel': '1123'}},{'name': 'stu4', 'school': '四川大學', 'tel': '9900', 'age': 30, 'score': 87, 'linkman': {'name': '小花', 'tel': '782'}},{'name': 'stu5', 'school': '西南交大', 'tel': '665', 'age': 22, 'score': 71, 'linkman': {'name': '老王', 'tel': '009'}},{'name': 'stu6', 'school': '成都理工大學', 'tel': '892', 'age': 32, 'score': 80, 'linkman': {'name': '老王2', 'tel': '0091'}},{'name': 'stu7', 'school': '四川大學', 'tel': '431', 'age': 17, 'score': 65, 'linkman': {'name': '老王3', 'tel': '0092'}},{'name': 'stu8', 'school': '攀枝花學院', 'tel': '2333', 'age': 16, 'score': 32, 'linkman': {'name': '老王4', 'tel': '0093'}},{'name': 'stu9', 'school': '攀枝花學院', 'tel': '565', 'age': 21, 'score': 71, 'linkman': {'name': '老王5', 'tel': '0094'}}] }1獲取班級位置
print(class1['address'])2)獲取班主任的名字和電話
print(class1['leader']['name'],class1['leader']['tel'])3)獲取所有學生的姓名和分數
for i in class1['students']:x = i['name']y = i['score']print(x, y)4)獲取所有學生聯系人的名字和電話
5)獲取班級最高分
scores = [] for i in class1['students']:x = i['score']if x not in scores:scores.append(x) scores.sort() print(scores[-1])6)獲取班級分數最高的學生的姓名
7)計算班級學生的平均分
scores = [] count = 0x = i['score']t += xif i != 0:count += 1 print(t/count)8)統計班級中未成年人數
count1 = 0for n in class1['students']:x = n['age']if x < 18:count1 += 1 print('未成年人數:',count1)9)用字典統計每個學校的人數, 類似: {'清華大學': 1, '攀枝花學院': 3}
總結
以上是生活随笔為你收集整理的day7-列表和字典作业的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光学字符识别
- 下一篇: 小狗的python代码_【Python】