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 = 0
    x = i['score']
    t += x
    if i != 0:
        count += 1
print(t/count)

8)统计班级中未成年人数

 count1 = 0
 for n in class1['students']:
     x = n['age']
     if x < 18:
         count1 += 1
print('未成年人数:',count1)

9)用字典统计每个学校的人数, 类似: {'清华大学': 1, '攀枝花学院': 3}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值