python写一个随机排班表,每次值班为24小时,分为员工和领导,员工116人,60个女员工,56个男员工,领导35人,15个男领导,20个女领导,116个员工属于10个部门,要求排班时男领导对应男员工,女领导对应女员工,并且员工只随机一次,每次值班时同部门的人要相隔一段时间
时间: 2023-11-26 19:03:01 浏览: 130
下面是一个简单的实现,使用了 Python 的 random 模块和 datetime 模块:
```python
import random
import datetime
# 定义员工和领导列表
employees = {
"male": ["员工1", "员工2", "员工3", ..., "员工56"],
"female": ["员工57", "员工58", "员工59", ..., "员工116"]
}
leaders = {
"male": ["领导1", "领导2", "领导3", ..., "领导15"],
"female": ["领导16", "领导17", "领导18", ..., "领导35"]
}
# 定义部门列表
departments = ["部门1", "部门2", "部门3", ..., "部门10"]
# 定义值班表
schedule = {}
# 定义值班开始日期
start_date = datetime.date(2022, 1, 1)
# 定义值班天数
days = 365
# 循环每个值班日期
for i in range(days):
# 计算当前值班日期
current_date = start_date + datetime.timedelta(days=i)
# 对于每个部门
for department in departments:
# 获取部门员工和领导列表
department_employees = employees["male"] + employees["female"]
department_leaders = leaders["male"] + leaders["female"]
# 如果是奇数天,领导先值班
if (i+1) % 2 == 1:
# 随机选择一个男领导和一个男员工
male_leader = random.choice(leaders["male"])
male_employee = random.choice(employees["male"])
# 从员工和领导列表中删除这两个人
department_leaders.remove(male_leader)
department_employees.remove(male_employee)
# 随机选择一个女领导和一个女员工
female_leader = random.choice(leaders["female"])
female_employee = random.choice(employees["female"])
# 从员工和领导列表中删除这两个人
department_leaders.remove(female_leader)
department_employees.remove(female_employee)
# 将四个人加入值班表
schedule[current_date] = {
department: [
{"leader": male_leader, "employee": male_employee},
{"leader": female_leader, "employee": female_employee}
]
}
# 如果是偶数天,员工先值班
else:
# 随机选择一个男员工和一个男领导
male_employee = random.choice(employees["male"])
male_leader = random.choice(leaders["male"])
# 从员工和领导列表中删除这两个人
department_employees.remove(male_employee)
department_leaders.remove(male_leader)
# 随机选择一个女员工和一个女领导
female_employee = random.choice(employees["female"])
female_leader = random.choice(leaders["female"])
# 从员工和领导列表中删除这两个人
department_employees.remove(female_employee)
department_leaders.remove(female_leader)
# 将四个人加入值班表
schedule[current_date] = {
department: [
{"employee": male_employee, "leader": male_leader},
{"employee": female_employee, "leader": female_leader}
]
}
# 将剩余员工和领导按照性别分组
remaining_employees = {
"male": department_employees[:len(department_employees)//2],
"female": department_employees[len(department_employees)//2:]
}
remaining_leaders = {
"male": department_leaders[:len(department_leaders)//2],
"female": department_leaders[len(department_leaders)//2:]
}
# 对于每个性别的员工和领导,随机分配一个值班日期
for gender in ["male", "female"]:
employees_or_leaders = ["employees", "leaders"]
random.shuffle(employees_or_leaders)
for person_type in employees_or_leaders:
for person in remaining_employees[gender] + remaining_leaders[gender]:
if person_type == "employees":
if gender == "male":
available_employees = remaining_employees["female"]
else:
available_employees = remaining_employees["male"]
if available_employees:
employee = random.choice(available_employees)
remaining_employees[gender].remove(employee)
schedule[current_date][department].append({"employee": person, "leader": employee})
else:
if gender == "male":
available_leaders = remaining_leaders["female"]
else:
available_leaders = remaining_leaders["male"]
if available_leaders:
leader = random.choice(available_leaders)
remaining_leaders[gender].remove(leader)
schedule[current_date][department].append({"leader": person, "employee": leader})
```
上述代码只是一个简单的实现,可能不满足所有的要求,您可以根据自己的实际情况进行修改。
阅读全文