Python Forum
Randomly assign values in List 1 to a value in List 2
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
Reply
#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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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?
Reply
#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?
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.
Reply
#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)]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Thanks for the replies everyone. These are helpful!
Reply
#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']
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.
Reply
#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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 220 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  pyaudio seems to randomly halt input. elpidiovaldez5 2 311 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Copying the order of another list with identical values gohanhango 7 1,061 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 578 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Comparing List values to get indexes Edward_ 7 1,082 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Adding values with reduce() function from the list of tuples kinimod 10 2,512 Jan-24-2023, 08:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020