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]
from random import randint
nums = [randint(95,100) for x in range(10)]
print(nums)
new_num = []
for item in nums:
if item not in new_num:
new_num.append(item)
print(new_num)
print(sorted(new_num, reverse=True))
2.利用列表推导式, 完成以下需求
a. 生成一个存放1-100中各位数为3的数据列表
结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
list_3 = [x for x in range(3,100,10)]
print(list_3)
b. 利用列表推到是将 列表中的整数提取出来
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
list1 = [True, 17, "hello", "bye", 98, 34, 21]
new_list1 = [item for item in list1 if type(item)==int]
print(new_list1)
c.利用列表推导式 存放指定列表中字符串的长度
例如 ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
list2 = ["good", "nice", "see you", "bye"]
new_list2 = [len(item) for item in list2]
print(new_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 item in class1['students']:
print('学生姓名:',item['name'],'分数:',item['score'])
4)获取所有学生联系人的名字和电话
item in class1['students']:
print('联系人姓名:',item['linkman']['name'],'电话:',item['linkman']['tel'])
5)获取班级最高分
print(max([item['score'] for item in class1['students']]))
6)获取班级分数最高的学生的姓名
stu_score = [item['score'] for item in class1['students']]
index_max = stu_score.index(max(stu_score))
print(class1['students'][index_max]['name'])
7)计算班级学生的平均分
stu_score = [item['score'] for item in class1['students']]
print('平均分:',sum(stu_score)/len(stu_score))
8)统计班级中未成年人数
count_age = 0
for item in class1['students']:
if item['age']<18:
count_age+=1
print('未成年的个数:',count_age)
9)用字典统计每个学校的人数, 类似: {'清华大学': 1, '攀枝花学院': 3}
school = {}
list_school = [item['school'] for item in class1['students']]
for item in list_school:
if not school.get(item):
school[item]=list_school.count(item)
print(school)