python排监考问题,请问怎么处理

img


29个人里面,有4个教语文,5个教数学,6个英语,3个物理,3个道法,3个历史,3个生物,2个体育,教语文的不能排语文,依次类推
多生成几个结果,这次排的时长高的,下次排少一些,另外一个excel文件,有什么好的思路

问题很好,下次不要问了


import random
import pandas as pd

s = ['语文'] * 4 \
    + ['数学'] * 5 \
    + ['英语'] * 6 \
    + ['物理'] * 3 \
    + ['道法'] * 3 \
    + ['生物'] * 3 \
    + ['历史'] * 3 \
    + ['体育'] * 2
teachers = [{'tm':0,'科目':_,'监考科目':[]} for _ in s]
场次 = {
    '语文' : [13,120],
    '英语' : [16,100],
    '数学' : [16,100],
    '物理' : [8,60],
    '道法' : [8,60],
    '历史' : [8,50],
    '化学' : [8,50]
      }
header = list(场次.keys())
plan = pd.DataFrame({'{}'.format(k):['' for _ in range(len(s))] for k in 场次})
plan.insert(0,'老师科目','',allow_duplicates=False)
plan.insert(1,'总用时','',allow_duplicates=False)
while len(场次) > 0:
    科目 = list(场次.keys())[0]
    场数,时间 = 场次[科目]
    teachers.sort(key = lambda x:(x['tm'],random.randint(1,200)))
    used = 0
    i = 0
    while used < 场数:
        if teachers[i]['科目'] != 科目:
            teachers[i]['监考科目'].append(科目)
            teachers[i]['tm'] += 时间
            used += 1
        i += 1
    del 场次[科目]
teachers.sort(key = lambda x:(x['科目'],x['tm']),reverse=True)
for i in range(len(s)):
    plan.loc[i,['老师科目','总用时']] = [teachers[i]['科目'],teachers[i]['tm']]
    for j in range(len(header)):
        if header[j] in teachers[i]['监考科目']:
            plan.loc[i,header[j]] = header[j]
plan.to_csv('考试安排.csv',encoding='utf_8_sig')