Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Randomly assign values in List 1 to a value in List 2
#1
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
Quote
#2
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)
Quote
#3
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?
Quote
#4
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?
SnoopFrogg likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#5
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)]
perfringo likes this post
Quote
#6
Thanks for the replies everyone. These are helpful!
Quote
#7
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']
buran likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#8
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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Add items from one list to list of lists PUP280 5 516 May-05-2020, 03:47 PM
Last Post: PUP280
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 306 Apr-10-2020, 02:39 PM
Last Post: buran
  Assign the sum of 2 consecutive numbers in a list to a varibale Fenaz 3 237 Apr-05-2020, 10:30 PM
Last Post: bowlofred
  appending list of list glennford49 2 219 Mar-29-2020, 09:33 AM
Last Post: ibreeden
  dot product of a list by a list in a dictionary stored as a value Fraher123 1 185 Mar-25-2020, 06:08 PM
Last Post: deanhystad
  Creating new list based on exact regex match in original list interjectdirector 1 207 Mar-08-2020, 09:30 PM
Last Post: deanhystad
  create a list of object with a list of character studenthch 0 157 Feb-12-2020, 08:43 AM
Last Post: studenthch
  how to compare a list to a list of lists kevthew 1 247 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 256 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  item = index position - list of list RavCOder 9 538 Dec-02-2019, 05:24 PM
Last Post: ThomasL

Forum Jump:


Users browsing this thread: 1 Guest(s)