Context: I have two lists of unequal size; names
holds family members’ names and chores
holds a much longer list of, well, chores. I am writing a program to randomly assign each chore to a family member, so that the everyone gets the same number of chores (or at least with +1/-1). I’ve thought of a few possible ways of going about this, at least in theory. One way would be to simply shuffle the list of chores
, split the list evenly into n
new lists, and assign one of these smaller lists to each family member. I could also loop through the list of chores, assigning each family member a chore on each pass through until all chores have been assigned to a family member.
I’ve had trouble finding specific operations or examples to help work through this; is there a specific workflow I should consider?.
Answer
Setup stolen from pakpe’s answer:
import random names = ['John', 'Ashley', 'Debbie'] chores = ['cook', 'clean', 'shop', 'pay bills', 'wash car', 'mow lawn', 'walk dog', 'drive kids']
Solution 1, distributing the chores evenly:
random.shuffle(names) random.shuffle(chores) assignments = {name: chores[i::len(names)] for i, name in enumerate(names)}
Sample result:
{'Debbie': ['wash car', 'clean', 'shop'], 'Ashley': ['walk dog', 'mow lawn', 'cook'], 'John': ['drive kids', 'pay bills']}
Solution 2, perhaps slightly easier but uneven (posted before they changed the question):
assignments = {name: [] for name in names} for chore in chores: assignments[random.choice(names)].append(chore)
Sample result:
{'Debbie': ['mow lawn', 'pay bills', 'shop', 'cook'], 'Ashley': [], 'John': ['wash car', 'walk dog', 'drive kids', 'clean']}
(First try, Ashley really got that lucky.)