目前各地学校都被要求阳光分班,也就是公平公正的为学生安排班级,但是从实现上,好多是教务员手工复制、粘贴完成的,本文作者系教师,较为了解一线教师的辛苦,这里呈现一种python语言的实现,前几日发给一位工作在小学的好友使用。
代码中实现了每个班人数最多与最少只能差1,保证每个班人数基本均衡。
缺点:没有考虑性别的均衡。
底部提供下载
import random
import PySimpleGUI as sg
import xlwt,os
import xlrd
#对每一位学生分班处理
def handlestudentone(rowlist,classnum,classmax_num,class_studentlists,yushu_mod):
tempno = random.randint(0, classnum - 1)
print('该{}班已有学生数:'.format(tempno + 1), len(class_studentlists[tempno]))
n=0 #达到最大人数的班级
for x in range(len(class_studentlists)):
if len(class_studentlists[x])==classmax_num:
n=n+1
if n==yushu_mod and len(class_studentlists[tempno]) == classmax_num-1:
return handlestudentone(rowlist, classnum, classmax_num, class_studentlists,yushu_mod)
if len(class_studentlists[tempno]) < classmax_num :
class_studentlists[tempno].append(rowlist)
return tempno+1
else:
return handlestudentone(rowlist, classnum, classmax_num, class_studentlists,yushu_mod)
#pysimplegui界面
sg.ChangeLookAndFeel('LightGreen')
cancel_btn = sg.Button('开始分班', size=(10, 2), font=("微软雅黑", 12))
layout = [
#[sg.Checkbox('3位数减法,退位,只能与竖式同时选择', size=(35, 2),text_color=('blue'), font=("微软雅黑", 12))],
# [sg.Slider(range=(1,5), orientation='h',size=(35, 10), font=("微软雅黑", 12))],
#[sg.Text('This is some text', font='Courier 12', text_color='blue', background_color='green')],
#[sg.Spin([1,2,3,4,5], size=(35, 10), font=("微软雅黑", 12))],
[sg.Text('设置班级数:',auto_size_text=True,size=(15, 1), font=("微软雅黑", 12))],
[sg.Slider(range=(1,22), orientation='h',size=(35, 15), font=("微软雅黑", 12))],
[sg.Text("导入学生数据:",auto_size_text=True,size=(15, 1), font=("微软雅黑", 12))], [sg.Input(), sg.FileBrowse("打开数据",file_types=(("Excel2003文件", "*.xls"),("Excel2007之后文件", "*.xlsx")))],
[ cancel_btn],[sg.StatusBar('作者: \n邮箱:45251289@qq.com\n版本日期:2020-8-17',size=(400,10), font=("微软雅黑", 12))]
]
window = sg.Window('学生随机分班器', default_element_size=(40, 2), size=(400, 285)).Layout(layout)
#处理界面事件
while True:
event, values = window.read()
if event in (None, '导入学生数据'):
# User closed the Window or hit the Cancel button
break
elif event in (None, '开始分班'):
#sg.Popup(event, values)
#print(values['打开数据'])
#print(values[0])
data = xlrd.open_workbook(values['打开数据'])
table = data.sheets()[0]
nrows = table.nrows # 获取该sheet中的有效行数
ncols= table.ncols# 获取该sheet中的有效列数
print(nrows)
wb = xlwt.Workbook()
ws = wb.add_sheet('分班结果', cell_overwrite_ok=True) # 增加sheet
for firstrow in range(0,ncols):
ws.write(0,firstrow,table.row(0)[firstrow].value)
ws.write(0, ncols,'分班结果')
totalstudentnum=nrows-1
classnum=int(values[0])
classmin_num=totalstudentnum//classnum
classmax_num=classmin_num+1
if totalstudentnum % classnum==0:
classmax_num=classmin_num
class_studentlists = [[] for j in range(classnum)]
#print(len(class_studentlists))
style = xlwt.XFStyle()
style.num_format_str = 'YYYY-MM-DD'
for i in range(1,nrows):
class_id=handlestudentone(table.row(i), classnum, classmax_num, class_studentlists,totalstudentnum % classnum)
for firstrow in range(0, ncols):
if table.row(i)[firstrow].ctype==3:
ws.write(i, firstrow, table.row(i)[firstrow].value,style)
ws.write(i, firstrow, table.row(i)[firstrow].value)
print("数据类型:", table.row(i)[firstrow].ctype)
ws.write(i, firstrow+1, "{}班".format(class_id))
for l in range(len(class_studentlists)):
print("{}班人数:".format(l+1),len(class_studentlists[l]))
wb.save('分班结果.xls')
os.startfile("分班结果.xls")
break
window.close()
经过pyinstaller打包成exe:下载链接