题目[1]:字典练习:
students = [
{'name': '张三', 'age': 18, 'score': 98, 'tel': '1388888998', 'gender': 'female'},
{'name': '李四', 'age': 28, 'score': 95, 'tel': '1388666666', 'gender': 'male'},
{'name': '王五', 'age': 21, 'score': 98, 'tel': '1365588889', 'gender': 'unknown'},
{'name': 'chris', 'age': 17, 'score': 47, 'tel': '13777775523', 'gender': 'male'},
{'name': 'jack', 'age': 23, 'score': 52, 'tel': '13999999928', 'gender': 'female'},
{'name': 'tony', 'age': 15, 'score': 89, 'tel': '1388888888', 'gender ': 'unknown'}
]
# (1)统计不及格学生的个数
# (2)打印不及格学生的名字和对应的成绩
# (3)统计未成年学生的个数
# (4)打印手机尾号是8的学生的名字
# (5)打印最高分和对应的学生的名字
# (6)删除性别不明的所有学生
# (7)将列表按学生成绩从大到小排序
1.统计不及格学生的个数
student_information = {
'stu': [
{'name': '张三', 'age': 18, 'score': 98, 'tel': '1388888998', 'gender': 'female'},
{'name': '李四', 'age': 28, 'score': 95, 'tel': '1388666666', 'gender': 'male'},
{'name': '王五', 'age': 21, 'score': 98, 'tel': '1365588889', 'gender': 'unknown'},
{'name': 'chris', 'age': 17, 'score': 47, 'tel': '13777775523', 'gender': 'male'},
{'name': 'jack', 'age': 23, 'score': 52, 'tel': '13999999928', 'gender': 'female'},
{'name': 'tony', 'age': 15, 'score': 89, 'tel': '1388888888', 'gender ': 'unknown'}
]
}
# 统计不及格学生的个数
count1 = 0 # 令初始个数为1
for x in student_information['stu']: # 用x对列表循环
if x['score'] < 60:
count1 += 1 #得分小于60 计数
print(count1) #打印输出
# 打印不及格学生的名字和对应的成绩
for x in student_information['stu']: # 用x对列表循环
if x['score'] < 60:
print(x['name'], x['score']) #得分小于60 输出姓名和成绩
# 统计未成年学生的个数
count2 = 0 # 令初始个数为1
for x in student_information['stu']: # 用x对列表循环
if x['age'] < 18:
count2 += 1 #年龄小于18 计数
print(count2) #打印输出
# 打印手机尾号是8的学生的名字
for x in student_information['stu']: # 用x对列表循环
if int(x['tel']) % 10 == 8: #电话号码对10取余,如果是8,即尾号
print(x['name']) #打印输出学生姓名
# 打印最高分和对应的学生的名字
max1 = 0 #默认最高分为0
list2 = [] #定义存储学生成绩空列表
for x in student_information['stu']: # 用x对列表循环
list2.append(x['score']) #先把所有学生成绩放在一个列表中
for y in list2:
if y > max1:
max1 = y #遍历列表,找到最高分
for z in student_information['stu']: # 用z对列表循环
if z['score'] == max1:
print(z['name'], max1) #找到最高分对应学生的姓名
# 删除性别不明的所有学生
for x in student_information['stu']: # 用x对列表循环
if x['gender'] == 'unknown': #找到性别未知的学生
x.clear() #调用clear()删去
for y in student_information['stu']: # 用x对列表循环
print(y) #打印已更新的列表
student_information = {
'stu': [
{'name': '张三', 'age': 18, 'score': 98, 'tel': '1388888998', 'gender': 'female'},
{'name': '李四', 'age': 28, 'score': 95, 'tel': '1388666666', 'gender': 'male'},
{'name': '王五', 'age': 21, 'score': 98, 'tel': '1365588889', 'gender': 'unknown'},
{'name': 'chris', 'age': 17, 'score': 47, 'tel': '13777775523', 'gender': 'male'},
{'name': 'jack', 'age': 23, 'score': 52, 'tel': '13999999928', 'gender': 'female'},
{'name': 'tony', 'age': 15, 'score': 89, 'tel': '1388888888', 'gender': 'unknown'}
]
}
# 将列表按学生成绩从大到小排序
list3 = []
for x in student_information['stu']:
list3.append(x['score'])
list3.sort(reverse=True)
print(list3)
for y in range(6):
for z in student_information['stu']: # 用z对列表循环
if z['score'] ==list3[y]:
题目[2]:随机生成10个,6位数数字验证码
运行上面的程序,并将程序改为列表推导式
运行:
修改后:
import random
code = [random.randint(100000,999999) for i in range(10)]
print(code)
截图:
题目[3]:随机生成10个,6位大写字母验证码
代码:
import random
import string #导入随机数和字符串
code = [] #存储校验码列表
for i in range(10):
x = ''
for j in range(6):
x = x + random.choice(string.ascii_uppercase)
code.append(x) # 生成的字母校验码追加到列表
print(code) #打印输出列表
运行:
题目[4]:随机生成10个,6位大写字母验证码,且不可重复。
代码:
import random
import string #导入随机数和字符串
code = [] #存储校验码列表
flag = 0 #标志出现重复校验码
for i in range(10):
x = ''
for j in range(6):
x = x + random.choice(string.ascii_uppercase)
for k in range(len(code)): #对之前生成的所有校验码遍历
if( x == code[k]):
flag = 1
break #如果发现有重复的,flag =1 结束循环
if (flag == 1):
break # 如果发现有重复的,跳过追加,重新生成
code.append(x) # 生成的字母校验码追加到列表
print(code) #打印输出列表