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]
法一
scores = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
count = len(scores)
for x in range(count):
y = scores.pop()
if y not in scores:
scores.insert(0, y)
print(scores)
new_scores = sorted(scores,reverse=True)
print(new_scores)
法二
nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
new_nums = []
for x in nums:
if x not in new_nums:
new_nums.append(x)
new_nums.sort(reverse=True)
2.利用列表推导式, 完成以下需求
a. 生成一个存放1-100中各位数为3的数据列表
结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
d = [x for x in range(3,100) if x % 10 ==3]
print(d)
b. 利用列表推到是将 列表中的整数提取出来
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
new_list1 = [x for x in list1 if type(x) == int]
print(new_list1) # [17, 98, 34, 21]
a = [True, 17, "hello", "bye", 98, 34, 21]
a1 = []
for x in a:
if type(x) == int:
a1.append(x)
print(a1)
c.利用列表推导式 存放指定列表中字符串的长度
例如 ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
list2 = ["good", "nice", "see you", "bye"]
list2_len = [len(x) for x in list2]
print(list2_len)
word = ["good", "nice", "see you", "bye"]
n_word = [len(x) for x in word]
print(n_word)
3.已知代码如下,请回答出各个print的结果 并说明原因
nums = [17, 39, 28, 51]
nums2 = nums
nums2.pop()
print(len(nums)) # 这个结果是什么 请描述原因
# 结果是3,因为pop()是删除列表中最后一个元素
numlist = [17, 22, 39, 58, [55, 43]]
nums3 = numlist.copy()
print(numlist is nums3) # 结果 原因是什么
# 结果是False,因为copy只是复制一个与原列表相同的列表,开辟了新的空间保存数据,因此不能说复制的新列表is原列表
numlist[-1][0] = 99
print(nums3) # num3会不会发生变化
# 会发生变化
4.定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.删除性别不明的所有学生
g.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students = [
{'name': '小明', 'age': 19, 'score': 89, 'tel': 1108, 'gender': '男'},
{'name': '小花', 'age': 20, 'score': 97, 'tel': 1104, 'gender': '女'},
{'name': '小华', 'age': 21,'score': 85, 'tel': 1102, 'gender': '不明'},
{'name': '小灯', 'age': 18, 'score': 78, 'tel': 1105, 'gender': '女'},
{'name': '小红', 'age': 17, 'score': 59, 'tel': 1101, 'gender': '不明'},
{'name': '小亮', 'age': 23, 'score': 88, 'tel': 1107, 'gender': '男'},
]
# a.统计不及格学生的个数
num = 0
for x in students:
if x['score'] < 60:
num += 1
print(num)
# b.打印不及格学生的名字和对应成绩
nam = 0
for x in students:
if x['score'] < 60:
print(x['name'], x['score'])
# c.统计未成年学生的个数
count = 0
for n in students:
if n['age'] < 18:
count += 1
print(count)
# d.打印手机尾号是八的学生的名字
法一:
for m in students:
if int(m['tel']) % 10 == 8:
print(m['name'])
法二:
result = [x['name'] for x in students if int(x['tel']) % 10 == 8]
print(result)
法三:
for stu in students:
tel = stu['tel']
if list(tel)[-1] == '8':
print(stu['name'])
# e.打印最高分和对应学生的名字
法一:
max1 = students[0] # 如果直接等于零的话不够严谨
for x in students:
if x['score'] > max1:
max1 = x
for x in students:
if x['score'] = max1:
print(x['score'], x['name'])
法二:(有点难)
max_score = max([x['score'] for x in students])
names = [x['name'] for x in students if x['score'] == max_score]
print(names)
法三(针对最高分只有一个的时候)
result = max(students, key=lambda x: x['age'])
print(result)
# f.删除性别不明的所有学生
'''a = 0
for a in students:
if a['gender'] == '不明':
students.remove(a)
print(students)
'''
# 有问题的代码:因为遍历的时候遍历不全导致删不干净
nums = [10, 89, 34, 30, 97]
for x in nums:
print(x)
if x < 60:
nums.remove(x)
print(nums) # [89, 30, 97]
"""
1. nums = [10, 89, 34, 30, 97]
2. for循环
x = 10: if 10<60 -> if True -> nums.remove(10) -> nums = [89, 34, 30, 97]
x = 34: if 34<60 -> if True -> nums.remove(34) -> nums = [89, 30, 97]
x = 97: if 97<60 -> if False
循环结束
3.print(nums)
"""
# 解决问题:
nums = [10, 89, 34, 30, 97]
temp = nums[:]
for x in temp:
if x < 60:
nums.remove(x)
print(nums)
# 将列表按学生成绩从大到小排序
def function(m):
print(m['score'])
return m['score']
students.sort(key=function, reverse=True)
print(students)
new_students = sorted(students, key=lambda x: x['score'], reverse=True)
print(new_students)