oplist.py
class Operatinglist():
def __init__(self,new_lists,old_lists):
# 已經分組list集合
self.new_lists = new_lists
# 未分組list
self.old_lists = old_lists
def has_new_list(self):
"""判斷是否有未分組的list"""
return self.old_list_size() != 0
def select_list_old_list(self,list):
"""list是否在old_list集合,如果有返回index,否則返回-1"""
if list is None:
return
if list in self.old_lists :
return self.old_lists.index(list)
else:
return -1
def del_new_add_old_list(self, index):
"""將一個list分組,並從old刪除返回並添加到new"""
if index is None:
return
else:
list = self.old_lists.pop(index)
self.new_lists.append(list)
return list
def add_old_lists(self, lists):
"""將一個新的list添加到未分組的list集合"""
for x in range(len(lists)):
self.old_lists.append(list[x])
def del_new_lists(self):
"""清空列表"""
self.new_lists.clear()
def new_list_size(self):
"""獲取未分組的list的集合大小"""
return len(self.new_lists)
def old_list_size(self):
"""獲取已經分組的list的集合大小"""
return len(self.old_lists)
opexcel.py
import xlrd
import xlwt
from xlutils.copy import copy
class Operatingexcel():
def get_excel_dic(self,filename,sheetname):
# filename 文件名
# sheetname 表單名
# 返回字典格式
dic = {}
data = xlrd.open_workbook(filename, 'r', encoding_override='utf-8')
table = data.sheet_by_name(sheetname)
for i in range(1, table.nrows):
for y in range(len(table.row_values(0))):
if table.row_values(i)[y] != "":
dic.setdefault(table.row_values(0)[y], []).append(table.row_values(i)[y])
return dic
def get_excel_list(self,filename,sheetname):
# filename 文件名
# sheetname 表單名
# 返回列表格式
list = []
data = xlrd.open_workbook(filename, 'r', encoding_override='utf-8')
table = data.sheet_by_name(sheetname)
for y in range(table.nrows):
for x in range(len(table.row_values(0))):
if table.row_values(y)[x] != "":
list.append(table.row_values(y)[x])
return list
def set_excel_dic(self,dic,filename,sheet_index,start_r):
# filename 文件名
# sheet_index第幾個工作表格
# start_r那一列
x = start_r
for k in dic.keys():
list = []
list.append(k)
for v in dic[k]:
list.append(v)
self.set_excel_list(list,filename,sheet_index,x)
x = x + 1
def set_excel_list(self,list,filename,sheet_index,start_r):
# filename 文件名
# sheet_index第幾個工作表格
# start_r那一列
# 讀取excel文件
r_xls = xlrd.open_workbook(filename)
# 將xlrd的對象轉化爲xlwt的對象
excel = copy(r_xls)
table = excel.get_sheet(sheet_index)
for y in range(len(list)):
table.write(y,start_r,str(list[y]))
excel.save(filename)
自動分組隨機選案.py
"""
author:魏振東
date:20200319
func:自動分組隨機選案軟件
齊齊哈爾地稅局需要一個自動分組隨機選案軟件,要求:
全市有1W多家企事業單位,需要隨機覈查;
地稅局有20人,包括3名科長,15名科員。要求隨機分組,不能人指定覈查單位。比如本次覈查要求分3組,每組4人,科長不能在一組;
分好小組,從1W多家企事業單位中每組隨機抽查5家覈查;
"""
from opdata.opexcel import Operatingexcel
import random
from opgroup.oplist import Operatinglist
def sub_group(old_lists,group,s_group):
# old_lists 需要分組的列表
# group 分幾組
# s_group 每組人數
# 返回字典類型
ol = Operatinglist([], old_lists,)
dic = {}
# 分成幾組
for i in range(group):
h = set()
# 每組分成幾個科員
while (len(h) < s_group):
# 隨機產生不重複的隨機數
h.add(random.randint(0, int(ol.old_list_size())))
for x in reversed(range(len(h))):
# 將x取出放入new_lists
dic.setdefault(i, []).append(ol.del_new_add_old_list(x))
return dic
if __name__ == '__main__':
# 獲取工作人員以及單位數據
og = Operatingexcel()
dic_excel = og.get_excel_dic("data/地稅局基本信息.xlsx", "工作人員")
list_excel = og.get_excel_list("data/地稅局基本信息.xlsx", "單位")
# 輸入數據
group = int(input("請輸入分組數"))
num_of_lead = int(input("請輸入每組科長人數"))
num_of_employ = int(input("請輸入每組科員人數"))
num_of_business = int(input("請輸入每組單位人數"))
# 判斷
while True:
if group == 0:
group = int(input("請重新輸入分組數"))
elif num_of_lead == 0 or num_of_lead * group > len(dic_excel["科長"]):
num_of_lead = int(input("請重新輸入每組科長人數"))
elif num_of_employ == 0 or num_of_employ * group > len(dic_excel["科員"]):
num_of_employ = int(input("請重新輸入每組科員人數"))
elif num_of_business == 0 or num_of_business * group > len(list_excel):
num_of_business = int(input("請重新輸入每組單位人數"))
else:
# 分組
dic1 = sub_group(dic_excel["科長"], group, num_of_lead)
dic2 = sub_group(dic_excel["科員"], group, num_of_employ)
dic3 = sub_group(list_excel, group, num_of_business)
# 保存入文件
for i in range(group):
dic = {}
dic.setdefault("科長", dic1[i])
dic.setdefault("科員", dic2[i])
dic.setdefault("用人單位", dic3[i])
og.set_excel_dic(dic, "data/分組明細.xlsx", i, 0)
break