# set集合综合案例

import random

list01 = []

for i inrange(100):

num01 =random.randint(0, 10)

if num01 not in list01:

list01.append(num01)

if len(list01)==5:

break

print(list01)

import random

number_set = set()

while len(number_set)< 5:

print(number_set)

C:\python\python.exe C:/python/demo/file3.py

{0, 1, 6, 9, 10}

Process finished with exit code 0

1）统计出每门科目的前三名和后三名【包含姓名和具体分数】

2）统计出总分的前三名和后三名

3）在（50-100）的数字中，那些数字没有在三门的分数中出现过

import random

student_name = ["王一", "胡二", "张三", "李四", "赵五", "马六", "杨七", "刘八", "孙九", "陈十"]

student_result = []  # 存储所有学生的成绩明细

chinese_result = set()  # 存储语文分数

maths_result = set()  # 存储数学分数

english_result = set()  # 存储外语分数

# 开始生成一个语文分数

for i in range(len(student_name)):

while True:

temp = random.randint(50, 100)

if temp not in chinese_result:

break

else:

continue

# 开始生成一个数学分数

while True:

temp = random.randint(50, 100)

if temp not in maths_result:

break

else:

continue

# 开始生成一个英语分数

while True:

temp = random.randint(50, 100)

if temp not in english_result:

break

else:

continue

print(chinese_result)

print(maths_result)

print(english_result)

import random

def get_result(result:set):

while True:

temp = random.randint(50, 100)

if temp not in result:

break

else:

continue

return result

student_name = ["王一", "胡二", "张三", "李四", "赵五", "马六", "杨七", "刘八", "孙九", "陈十"]

student_result = []  # 存储所有学生的成绩明细

chinese_result = set()  # 存储语文分数

maths_result = set()  # 存储数学分数

english_result = set()  # 存储外语分数

# 开始生成分数

for i in range(len(student_name)):

# 开始生成一个语文分数

chinese_result =get_result(chinese_result)

# 开始生成一个数学分数

maths_result = get_result(maths_result)

# 开始生成一个英语分数

english_result =get_result(english_result)

print(student_name)

print(chinese_result)

print(maths_result)

print(english_result)

# 把三个set集合转为list

chinese_result = list(chinese_result)

maths_result = list(maths_result)

english_result = list(english_result)

# 生成成绩明细

for i in range(len(student_name)):

temp_list = []

temp_list.append(chinese_result[i])

temp_list.append(maths_result[i])

temp_list.append(english_result[i])

student_result.append(temp_list)

print(chinese_result)

print(maths_result)

print(english_result)

print(student_result)

# 需求1：统计出每门科目的前三名和后三名【包含姓名和具体分数】以语文成绩为例

chinese_one = max(chinese_result)

print("语文第一名：姓名：%s,分数：%d" %(student_name[chinese_result.index(chinese_one)],chinese_one))

chinese_two = sorted(chinese_result)[8]

print("语文第二名：姓名：%s,分数：%d" %(student_name[chinese_result.index(chinese_two)],chinese_two))

chinese_three = sorted(chinese_result)[7]

print("语文第三名：姓名：%s,分数：%d" %(student_name[chinese_result.index(chinese_three)],chinese_three))

chinese_last_one = sorted(chinese_result)[0]

print("语文倒数第一名：姓名：%s,分数：%d" %(student_name[chinese_result.index(chinese_last_one)],chinese_last_one))

chinese_last_two = sorted(chinese_result)[1]

print("语文倒数第二名：姓名：%s,分数：%d" %(student_name[chinese_result.index(chinese_last_two)],chinese_last_two))

chinese_last_three = sorted(chinese_result)[2]

print("语文倒数第三名：姓名：%s,分数：%d" %(student_name[chinese_result.index(chinese_last_three)],chinese_last_three))

# 需求2：统计出总分的前三名和后三名

student_total_result = []

for i instudent_result:

student_total_result.append(sum(i))

print(student_total_result)

# 打印前三名

total_one = sorted(student_total_result)[9# 获取第一名分数

total_one_index = student_total_result.index(total_one)  # 获取第一名分数的索引

# 打印第一名的姓名和总分均分

print("总分第一名【姓名：%s,分数：%d,均分：%.2f" %(student_name[total_one_index],total_one,total_one/3))

# 打印第一名的分数明细

print("\t\t 语文：%d,数学：%d,英语：%d" % (student_result[total_one_index][0], student_result[total_one_index][1],student_result[total_one_index][2]))

total_two = sorted(student_total_result)[8]

print("总分第二名【姓名：%s,分数：%d,均分：%.2f" %(student_name[student_total_result.index(total_two)],total_two,total_two/3))

total_three = sorted(student_total_result)[7]

print("总分第三名【姓名：%s,分数：%d,均分：%.2f" %(student_name[student_total_result.index(total_three)],total_three,total_three/3))

# 需求3：在（50-100）的数字中，那些数字没有在三门的分数中出现过

total_number = set()

for i inrange(50,101):

had_number = (set(chinese_result) | set(maths_result) | set(english_result))

C:\python\python.exe C:/python/demo/file3.py

['王一', '胡二','张三','李四','赵五','马六','杨七','刘八','孙九','陈十']

{66, 80, 83, 51, 52, 86, 54, 55, 60,94}

{98, 69, 74, 75, 77, 82, 52, 89, 92,95}

{96, 66, 71, 72, 85, 88, 92, 61, 95,63}

[66, 80, 83, 51, 52, 86, 54, 55, 60,94]

[98, 69, 74, 75, 77, 82, 52, 89, 92,95]

[96, 66, 71, 72, 85, 88, 92, 61, 95,63]

[[66, 98, 96], [80, 69, 66], [83, 74,71], [51, 75, 72], [52, 77, 85], [86, 82, 88], [54, 52, 92], [55, 89, 61], [60,92, 95], [94, 95, 63]]

[260, 215, 228, 198, 214, 256, 198,205, 247, 252]

语文：66,数学：98,英语：96

Process finished with exit code 0

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120