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]
nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
new_nums = [] # 建立一个新列表存储数字
for num in nums: # 按个取出nums中的元素
if num not in new_nums: # 判断取出原数是否重复
new_nums.append(num) # 添加进new_nums
new_nums.sort(reverse=True) # 降序排序
print(new_nums)
2.利用列表推导式, 完成以下需求
a. 生成一个存放1-100中各位数为3的数据列表
结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
print([x for x in range(100) if x % 10 == 3])
b. 利用列表推导式将 列表中的整数提取出来
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
print([x for x in string if type(x) == int])
c.利用列表推导式 存放指定列表中字符串的长度
例如 ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
print([len(x) for x in string])
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']['tel'])
3)获取所有学生的姓名和分数
for stu in class1['students']:
print(stu['name'], stu['score'])
4)获取所有学生联系人的名字和电话
for stu in class1['students']:
print(stu['linkman']['name'], stu['linkman']['tel'])
5)获取班级最高分
# 方法一:
scores = []
for stu in class1['students']:
scores.append(stu['score'])
print(max(scores))
# 方法二:
max_score = 0
for stu in class1['students']:
if stu['score'] >= max_score:
max_score = stu['score']
print(max_score)
6)获取班级分数最高的学生的姓名
_stu = 0
for stu in class1['students']:
if stu['score'] >= max_score:
max_score = stu['score']
_stu = stu
print(_stu['name'])
7)计算班级学生的平均分
scores = []
for stu in class1['students']:
scores.append(stu['score'])
print(sum(scores)/len(scores))
8)统计班级中未成年人数
count = 0
for stu in class1['students']:
if stu['age'] < 18:
count += 1
print(count)
9)用字典统计每个学校的人数, 类似: {'清华大学': 1, '攀枝花学院': 3}
# 方法一:
schools = {}
for stu in class1['students']: # 依次取出学生信息
schools[stu['school']] = 0 # 添加学生所在学校
for stu in class1['students']: # 依次取出学生信息
schools[stu['school']] += 1 # 判断学校人数
print(schools)
方法二:
schools = {}
for stu in class1['students']:
schools[stu['school']] = 0
if stu['school'] in schools:
schools[stu['school']] += 1
print(schools)