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
#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


Messages In This Thread
RE: Randomly assign values in List 1 to a value in List 2 - by perfringo - Dec-11-2019, 12:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Assigning cycle values in a list nmancini 3 1,123 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  remove duplicates from dicts with list values wardancer84 27 6,540 May-27-2024, 04:54 PM
Last Post: wardancer84
  Strange behavior list of list mmhmjanssen 3 1,759 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 2,196 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  pyaudio seems to randomly halt input. elpidiovaldez5 2 1,602 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Copying the order of another list with identical values gohanhango 7 2,858 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 3,024 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 2,774 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 1,536 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Comparing List values to get indexes Edward_ 7 3,691 Jun-09-2023, 04:57 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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