首先這是我們的抽獎名單(欄位名稱、欄位數量與內容不限,可自由調整增加)
這是我們的獎品清單(欄位名稱需固定,內容可自由調整)
抽獎程式如下
import pandas as pd
import random
#匯入抽獎名單
name_list = pd.read_excel("抽獎名單.xlsx")
#匯入獎品清單
prize_list = pd.read_excel("獎品清單.xlsx")
import pandas as pd
import random
#------------------------------------------------------------
#匯入抽獎名單
name_list = pd.read_excel("抽獎名單.xlsx")
#匯入獎品清單
prize_list = pd.read_excel("獎品清單.xlsx")
#------------------------------------------------------------
#是否可在【整場抽獎】中重複中獎(若為False則為不可重複中獎,True為可以重複中獎)
reap_winning_in_game = False
#是否可在同一【獎項類別】內重複中獎(若為False則為不可重複中獎,True為可以重複中獎)
reap_winning_in_prize_class = False
#是否可在同一【獎項名稱】內重複中獎(若為False則為不可重複中獎,True為可以重複中獎)
reap_winning_in_prize_name = False
#------------------------------------------------------------
#【整場抽獎】中獎人_list
winner_sheet_all_list = []
#【整場抽獎】中獎序號_list
winner_list_in_game = []
#【整場抽獎】中獎人_list
winner_sheet_all_list = []
#計算抽獎名單之人數
name_list_count = len(name_list)
#在每個【獎項類別】內:
for prize_class in prize_list["獎項類別"].unique():
#在該【獎項類別】中:
prize_list_ing = prize_list[prize_list["獎項類別"] == prize_class]
#在該【獎項類別】中之中獎序號_list
winner_list_in_prize_class = []
#在該【獎項類別】中,的每個獎品名稱內:
for prize_name,num_of_prizes in zip(prize_list_ing["獎項名稱"],prize_list_ing["獎項數量"]):
#在該【獎項名稱】中之中獎序號_list
winner_list_in_prize_name = []
#如果不可在【整場抽獎】內重複中獎:
if reap_winning_in_game == False:
#持續做抽獎迴圈,直到該【獎項名稱】中獎序號_list內數量=該獎品數量:
while len(winner_list_in_prize_name) < num_of_prizes:
#隨機抽出中獎序號
num = random.randint(0,name_list_count-1)
#如果抽出的序號不在【整場抽獎】中獎序號_list中,才將該序號放入該【獎項名稱】中獎序號_list
if (num not in winner_list_in_game):
winner_list_in_prize_name.append(num)
winner_list_in_prize_class.append(num)
winner_list_in_game.append(num)
#如果可以在【整場抽獎】內重複:
elif reap_winning_in_game == True:
#如果不可在同一【獎項名稱】內重複 且 不可在同一【獎項類別】內重複:
if (reap_winning_in_prize_name == False and reap_winning_in_prize_class == False):
while len(winner_list_in_prize_name) < num_of_prizes:
num = random.randint(0,name_list_count-1)
if (num not in winner_list_in_prize_name and num not in winner_list_in_prize_class):
winner_list_in_prize_name.append(num)
winner_list_in_prize_class.append(num)
winner_list_in_game.append(num)
#如果不可在同一【獎項名稱】內重複 但 可以在同一【獎項類別】內重複:
elif (reap_winning_in_prize_name == False and reap_winning_in_prize_class == True):
while len(winner_list_in_prize_name) < num_of_prizes:
num = random.randint(0,name_list_count-1)
if num not in winner_list_in_prize_name:
winner_list_in_prize_name.append(num)
winner_list_in_prize_class.append(num)
winner_list_in_game.append(num)
#如果可以在同一【獎項名稱】內重複 但 不可在同一【獎項類別】內重複:
elif (reap_winning_in_prize_name == True and reap_winning_in_prize_class == False):
while len(winner_list_in_prize_name) < num_of_prizes:
num = random.randint(0,name_list_count-1)
#如果抽出的序號不在【獎項名稱】中獎序號_list中,將該序號放入【獎項名稱】中獎序號_list
if num not in winner_list_in_prize_class:
winner_list_in_prize_name.append(num)
winner_list_in_prize_class.append(num)
winner_list_in_game.append(num)
#如果抽出的序號不在【獎項類別】中獎序號_list中,但已經存在在【獎項名稱】中獎序號名單中,將該序號放入【獎項名稱】中獎序號_list
elif (num in winner_list_in_prize_class and num in winner_list_in_prize_name):
winner_list_in_prize_name.append(num)
winner_list_in_prize_class.append(num)
winner_list_in_game.append(num)
#如果可以在同一【獎項名稱】內重複 且 可以在同一獎項類別內重複:
elif (reap_winning_in_prize_name == True and reap_winning_in_prize_class == True):
while len(winner_list_in_prize_name) < num_of_prizes:
num = random.randint(0,name_list_count-1)
winner_list_in_prize_name.append(num)
winner_list_in_prize_class.append(num)
winner_list_in_game.append(num)
#建立一個list,將抽獎名單中,所有在該【獎項名稱】中獎序號_list上的人的明細資料篩選出來,並放入此list
winner_sheet_list = []
for i in winner_list_in_prize_name:
globals()[f"winner_sheet_list_{i}"] = name_list.iloc[i:i+1,:]
winner_sheet_list.append(globals()[f"winner_sheet_list_{i}"])
#將該list上之中獎人明細資料,聚合成一張該【獎項名稱】中獎人清單
globals()[f"winner_sheet_{prize_name}"] = pd.concat(winner_sheet_list,axis=0)
#將該【獎項名稱】中獎人清單加上獎品類別、獎項名稱欄位
globals()[f"winner_sheet_{prize_name}"]["獎項類別"] = prize_class
globals()[f"winner_sheet_{prize_name}"]["獎項名稱"] = prize_name
#將該【獎項名稱】中獎人清單,放入【整場抽獎】中獎人_list
winner_sheet_all_list.append(globals()[f"winner_sheet_{prize_name}"])
#將【整場抽獎】中獎人_list的所有【獎項名稱】中獎人清單,合併為全體中獎人清單
winner_sheet_all = pd.concat(winner_sheet_all_list,axis=0)
#------------------------------------------------------------
#匯出中獎名單
winner_sheet_all.to_excel("中獎名單.xlsx")
中獎名單結果如下: