![]() |
Randomly assign values in List 1 to a value in List 2 - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Randomly assign values in List 1 to a value in List 2 (/thread-23076.html) |
Randomly assign values in List 1 to a value in List 2 - Seneca260 - Dec-10-2019 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 RE: Randomly assign values in List 1 to a value in List 2 - buran - Dec-10-2019 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) RE: Randomly assign values in List 1 to a value in List 2 - Seneca260 - Dec-11-2019 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? RE: Randomly assign values in List 1 to a value in List 2 - perfringo - Dec-11-2019 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? RE: Randomly assign values in List 1 to a value in List 2 - buran - Dec-11-2019 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())
RE: Randomly assign values in List 1 to a value in List 2 - Seneca260 - Dec-12-2019 Thanks for the replies everyone. These are helpful! RE: Randomly assign values in List 1 to a value in List 2 - perfringo - Dec-12-2019 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'] RE: Randomly assign values in List 1 to a value in List 2 - BamBi25 - Dec-16-2019 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) |