Python Forum

Full Version: Randomly assign values in List 1 to a value in List 2
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I am learning Python and also teaching a class and I want to build a program that random assigns students a day to do their presentation.

dates = ['Jan 6', 'Jan 8', 'Jan 10']
students = ['Joey,' 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle', 'Marcus', 'Pat']
I am trying to randomly assign students to one of the days to do a presentation. I would thus want an item from List 1 to match with an item in List 2, but am unsure of how to do this.

Thanks in advance
probably you also want to put some constrains - i.e. max number of presentations per day/in each different day if they vary.You don't want to have all presentations in a single day (extreme case, but possible if no other constrains)
Thanks buran,

Yes, precisely. If they could be evenly distribute that would be great. I am just wondering how do I actually code this? I have been trying to find this online, but whenever I search Google or Stack Overflow, I can't seem to get it. Any suggestions on how to search for this or what code would be most appropriate?
If I understand the objective correctly then my approach would be:

- shuffle the list
- slice list to evenly distributed chunks
- assign chunks to dates

Something like that:

>>> import random
>>> students = ['Joey', 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle', 'Marcus', 'Pat']
>>> dates = ['Jan 6', 'Jan 8', 'Jan 10']
>>> step = len(students) // len(dates)
>>> random.shuffle(students)        # shuffles list in place
>>> students
['Paul', 'Salvator', 'Daniel', 'Mary', 'Pat', 'Jose', 'Marcus', 'Steve', 'James', 'Samantha', 'Joey', 'Henry', 'Kyle']
>>> assignments = dict()
>>> for i, date in enumerate(dates):
...     assignments[date] = students[i*step:i*step+step]
... 
>>> assignments
{'Jan 6': ['Paul', 'Salvator', 'Daniel', 'Mary'], 'Jan 8': ['Pat', 'Jose', 'Marcus', 'Steve'], 'Jan 10': ['James', 'Samantha', 'Joey', 'Henry']}
It assumes that step is integer i.e. there are even numbers of students for each day (currently 'Kyle' is left out). Should he be added to first or last or randomly? Are there constraints how many students can present in one day?
there are plenty of possibilities, e.g.
from itertools import cycle
from collections import Counter
from random import shuffle

students = ['Joey', 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle', 'Marcus', 'Pat']
dates = ['Jan 6', 'Jan 8', 'Jan 10']

shuffle(students)
schedule = dict(zip(students, cycle(dates)))
print(schedule)
print(Counter(schedule.values()).most_common())
Output:
{'Steve': 'Jan 6', 'Mary': 'Jan 8', 'Kyle': 'Jan 10', 'Jose': 'Jan 6', 'Henry': 'Jan 8', 'Marcus': 'Jan 10', 'Paul': 'Jan 6', 'James': 'Jan 8', 'Salvator': 'Jan 10', 'Samantha': 'Jan 6', 'Joey': 'Jan 8', 'Pat': 'Jan 10', 'Daniel': 'Jan 6'} [('Jan 6', 5), ('Jan 8', 4), ('Jan 10', 4)]
Thanks for the replies everyone. These are helpful!
buran code can be extended to get list of students by dates. We have dictionary of students and we just reverse keys and values:

>>> from collections import defaultdict
>>> schedule = {'Steve': 'Jan 6', 'Mary': 'Jan 8', 'Kyle': 'Jan 10', 'Jose': 'Jan 6', 'Henry': 'Jan 8', 'Marcus': 'Jan 10', 'Paul': 'Jan 6', 'James': 'Jan 8', 'Salvator': 'Jan 10', 'Samantha': 'Jan 6', 'Joey': 'Jan 8', 'Pat': 'Jan 10', 'Daniel': 'Jan 6'}
>>> by_dates = defaultdict(list)
>>> for name, date in schedule.items():
...     by_dates[date].append(name)
... 
>>> by_dates
defaultdict(<class 'list'>, {'Jan 6': ['Steve', 'Jose', 'Paul', 'Samantha', 'Daniel'], 'Jan 8': ['Mary', 'Henry', 'James', 'Joey'], 'Jan 10': ['Kyle', 'Marcus', 'Salvator', 'Pat']})
>>> dict(by_dates)
{'Jan 6': ['Steve', 'Jose', 'Paul', 'Samantha', 'Daniel'], 'Jan 8': ['Mary', 'Henry', 'James', 'Joey'], 'Jan 10': ['Kyle', 'Marcus', 'Salvator', 'Pat']}
>>> by_dates['Jan 8']
['Mary', 'Henry', 'James', 'Joey']
Another one :p

import random

dates = ['Jan 6', 'Jan 8', 'Jan 10']
students = ['Joey', 'Henry', 'Daniel', 'James', 'Samantha', 'Jose', 'Salvator', 'Paul', 'Steve', 'Mary', 'Kyle',
            'Marcus', 'Pat']
Nstud = (len(students) // len(dates))

l = [students[i * Nstud:((i + 1) * Nstud)] for i in range(len(students) // Nstud)]
final = {date: l[i] for i, date in enumerate(dates)}

print(final)