set集合綜合案例

案例01:生成0-10之間5個不相等的數

方法01:使用list集合實現

import random

list01 = []

for i in range(100):

    num01 = random.randint(0, 10)

    if num01 not in list01:

        list01.append(num01)

    if len(list01)==5:

        break

print(list01)

 

方法01:使用set集合實現,自動消除重複

import random

number_set = set()

while len(number_set) < 5:

    number_set.add(random.randint(0,10))

 

print(number_set)

 

執行結果:

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

{0, 1, 6, 9, 10}

 

Process finished with exit code 0

 

案例02:有10個學生,姓名自行添加。有三門考試:語文、數學和英語,隨機爲這10名學生生成分數【50-100】,要求每一門科目中所有學生分數不能重複

需求:

1)統計出每門科目的前三名和後三名【包含姓名和具體分數】

2)統計出總分的前三名和後三名

3)在(50-100)的數字中,那些數字沒有在三門的分數中出現過

 

方法01

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:

            chinese_result.add(temp)

            break

        else:

            continue

    # 開始生成一個數學分數

    while True:

        temp = random.randint(50, 100)

        if temp not in maths_result:

            maths_result.add(temp)

            break

        else:

            continue

    # 開始生成一個英語分數

    while True:

        temp = random.randint(50, 100)

        if temp not in english_result:

            english_result.add(temp)

            break

        else:

            continue

 

print(chinese_result)

print(maths_result)

print(english_result)

方法02

對於相同的操作可以抽象成一個函數,在通過get調用這個函數即可

import random

def get_result(result:set):

    while True:

        temp = random.randint(50, 100)

        if temp not in result:

            result.add(temp)

            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 in student_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 in range(50, 101):

    total_number.add(i)

 

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

print("未出現的數字有:", (total_number-had_number))

 

執行結果:

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]]

語文第一名:姓名:陳十,分數:94

語文第二名:姓名:馬六,分數:86

語文第三名:姓名:張三,分數:83

語文倒數第一名:姓名:李四,分數:51

語文倒數第二名:姓名:趙五,分數:52

語文倒數第三名:姓名:楊七,分數:54

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

總分第一名【姓名:王一,分數:260,均分:86.67

 語文:66,數學:98,英語:96

總分第二名【姓名:馬六,分數:256,均分:85.33

總分第三名【姓名:陳十,分數:252,均分:84.00

未出現的數字有: {50, 53, 56, 57, 58, 59, 62, 64, 65, 67, 68, 70, 73, 76, 78, 79, 81, 84, 87, 90, 91, 93, 97, 99, 100}

 

Process finished with exit code 0


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章