首页 > 解决方案 > 根据来自例如涂鸦投票的可用性自动将参与者分配给事件

问题描述

我在 pandas DataFrame ( poll_result) 中得到了以下(随机)类似涂鸦的民意调查结果:

import random
import names

choices = ['no','yes','Under reserve']

number_of_participants = 30
number_of_events = 6


availabilities = { names.get_full_name() : random.choices(population=choices, weights=[0.7, 0.2, 0.1], k=number_of_events) for participant in range(number_of_participants) }

poll_result = pd.DataFrame.from_dict(availabilities,orient='index')

我已经设法将人员分配到事件中,如下所示:

remaining_people = list(poll_result.index)

allocation = {}

for c in poll_result.columns:
    allocation[c] = []
    for rp in remaining_people:
        if poll_result.loc[rp,c] == "yes":
            allocation[c].append(rp)
            remaining_people.remove(rp)

结果参与者名单:

for event, participants in allocation.items():
    print(f"Event {event}:")
    for n,p in enumerate(participants,1):
        print(f"\t{n}. {p}")

print(f"\n\nPeople not allocated: {len(remaining_people)} ({', '.join(remaining_people)})")

有没有更复杂的方法来实现优化分配(最少的活动或每个活动最多的参与者)?

标签: pythonoptimization

解决方案


推荐阅读