python自动排班表

前言

实现对多个人在一周的自动排班表,要求每行每列不能有重复的。排好班之后,以excel的形式输出。

解决方案

  1. 首先输出每行每列不重名的一个矩阵
  2. 统计每个人在一周排班中出现的次数
  3. 将矩阵数据写入excel中并保存

代码实现

from random import shuffle
from random import sample
import numpy as np
import xlwt


name_list = ['田','兰','刘','杨','余','唐','于','莫','吴','乔','敏','童','杜','王','豆','黄','苹','磊','曼','陈','皱','谢','果','汪','麒']
name_list_size = len(name_list)

print("The are total {} people".format(name_list_size))

def get_matrix():
	while 1:

	    row_1 = sample(name_list, 5)
	    row_2 = sample(name_list, 5)
	    row_3 = sample(name_list, 5)
	    row_4 = sample(name_list, 5)
	    row_5 = sample(name_list, 5)
	    row_6 = sample(name_list, 5)
	    row_7 = sample(name_list, 5)
	    row_8 = sample(name_list, 5)
	    row_9 = sample(name_list, 5)
	    row_10 = sample(name_list, 5)

	#get a matrix for 10x5 array
	    matrix = np.array([row_1,row_2,row_3,row_4,row_5,row_6,row_7,row_8,row_9,row_10])

	    col_1 = matrix[:,0]
	    col_2 = matrix[:,1]
	    col_3 = matrix[:,2]
	    col_4 = matrix[:,3]
	    col_5 = matrix[:,4]

	    set_col_1 = set(col_1)
	    set_col_2 = set(col_2)
	    set_col_3 = set(col_3)
	    set_col_4 = set(col_4)
	    set_col_5 = set(col_5)

		#check whether all people has been in the list
	    unique_list = []
	    for i in range(10):
	        for j in range(5):
	            if matrix[i][j] not in unique_list:
	                unique_list.append(matrix[i][j])

	    #calculate repeated times for every member
	    matrix_list_all_num = []
	    for h in range(10):
	    	for k in range(5):
	    		matrix_list_all_num.append(matrix[h][k])

	    dic = {}
	    for item in unique_list:
	    	dic.update({item: matrix_list_all_num.count(item)})

	    if len(unique_list) == len(name_list) and len(set_col_1) == len(col_1) and len(set_col_2) == len(col_2) and len(set_col_3) == len(col_3) and len(set_col_4) == len(col_4) and len(set_col_5) == len(col_5):
	       #print("Totally calculate", i, "times\n")
	       print("All people are scheduled\n")
	       print(matrix)
	       print("The repeat time for every member\n")
	       print(dic)
	       break
	return matrix

def data_write(file_path, datas):
	book = xlwt.Workbook()
	sheet = book.add_sheet('sheet1', cell_overwrite_ok=True)

	sheet.write(0, 0, '星期一')
	sheet.write(0, 1, '星期二')
	sheet.write(0, 2, '星期三')
	sheet.write(0, 3, '星期四')
	sheet.write(0, 4, '星期五')
	#sheet.write(0, 1, '星期六')


	i = 1
	for data in datas:
		for j in range(len(data)):
			sheet.write(i, j, data[j])
		i = i + 1

	book.save(file_path)

matrix_copy = get_matrix()
book_name_xls = 'schedule_shift.xls'

data_write(book_name_xls, matrix_copy)

version_2

增加了每个人在一个周内排班的总次数到excel中

from random import shuffle
from random import sample
import numpy as np
import xlwt


name_list = ['田','兰','刘','杨','余','唐','于','莫','吴','乔','敏','童','杜','王','豆','黄','苹','磊','曼','陈','皱','谢','果','汪','麒']
name_list_size = len(name_list)

print("The are total {} people".format(name_list_size))

#to convert the name_list to set(name_list)
#unique_list = []
#to store the member to dic
#dic = {}

def get_matrix():
	while 1:

	    row_1 = sample(name_list, 5)
	    row_2 = sample(name_list, 5)
	    row_3 = sample(name_list, 5)
	    row_4 = sample(name_list, 5)
	    row_5 = sample(name_list, 5)
	    row_6 = sample(name_list, 5)
	    row_7 = sample(name_list, 5)
	    row_8 = sample(name_list, 5)
	    row_9 = sample(name_list, 5)
	    row_10 = sample(name_list, 5)

	#get a matrix for 10x5 array
	    matrix = np.array([row_1,row_2,row_3,row_4,row_5,row_6,row_7,row_8,row_9,row_10])

	    col_1 = matrix[:,0]
	    col_2 = matrix[:,1]
	    col_3 = matrix[:,2]
	    col_4 = matrix[:,3]
	    col_5 = matrix[:,4]

	    set_col_1 = set(col_1)
	    set_col_2 = set(col_2)
	    set_col_3 = set(col_3)
	    set_col_4 = set(col_4)
	    set_col_5 = set(col_5)

		#check whether all people has been in the list
	    unique_list = []
	    for i in range(10):
	        for j in range(5):
	            if matrix[i][j] not in unique_list:
	                unique_list.append(matrix[i][j])

	    #calculate repeated times for every member
	    matrix_list_all_num = []
	    for h in range(10):
	    	for k in range(5):
	    		matrix_list_all_num.append(matrix[h][k])

	    dic = {}
	    for item in unique_list:
	    	dic.update({item: matrix_list_all_num.count(item)})

	    #generate the excel to save table
	    book_name_xls = 'schedule_shift.xls'

	    static_key_list = list(dic)
	    static_value_list = list(dic.values())

	    if len(unique_list) == len(name_list) and len(set_col_1) == len(col_1) and len(set_col_2) == len(col_2) and len(set_col_3) == len(col_3) and len(set_col_4) == len(col_4) and len(set_col_5) == len(col_5):
	       print("All people are scheduled\n")
	       print(matrix)

	       #generate the excel
	       book = xlwt.Workbook()
	       sheet = book.add_sheet('sheet', cell_overwrite_ok=True)

	       sheet.write(0, 0, '星期一')
	       sheet.write(0, 1, '星期二')
	       sheet.write(0, 2, '星期三')
	       sheet.write(0, 3, '星期四')
	       sheet.write(0, 4, '星期五')
	       #sheet.write(0, 1, '星期六')

	       i = 1
	       
	       for data in matrix:
	            for j in range(len(data)):
	                sheet.write(i, j, data[j])
	            i = i + 1
	            book.save(book_name_xls)

	       j = 13
	       for item in static_key_list:
	       	sheet.write(j, 0, item)
	       	j = j + 1

	       k = 13
	       for item1 in static_value_list:
	       	sheet.write(k, 1, item1)
	       	k = k + 1

	       book.save(book_name_xls)

	       print('\n')
	       print("The repeat time for every member\n")
	       print(dic)
	       break
	return matrix

get_matrix()
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值